René's URL Explorer Experiment


Title: HashMap的几种初始化方法 · Issue #2 · Sitrone/LeetcodeInJava · GitHub

Open Graph Title: HashMap的几种初始化方法 · Issue #2 · Sitrone/LeetcodeInJava

X Title: HashMap的几种初始化方法 · Issue #2 · Sitrone/LeetcodeInJava

Description: ArrayList初始化方法 常规方法 List list = new ArrayList(); list.add("string1"); list.add("string2"); //some other list.add() code...... list.add("stringN"); 使用Arrrays.asList()方法 ArrayList list = new ArrayList(Array...

Open Graph Description: ArrayList初始化方法 常规方法 List list = new ArrayList(); list.add("string1"); list.add("string2"); //some other list.add() code...... list.add("stringN"); 使用Arrrays.asList()方法 Arra...

X Description: ArrayList初始化方法 常规方法 List<string> list = new ArrayList</string><string>(); list.add("string1"); list.add("string2"); //some other list.add() code...... list.add...

Opengraph URL: https://github.com/Sitrone/LeetcodeInJava/issues/2

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"HashMap的几种初始化方法","articleBody":"## ArrayList初始化方法\n1. 常规方法  \n\n``` java\nList\u003cstring\u003e list = new ArrayList\u003c/string\u003e\u003cstring\u003e();  \nlist.add(\"string1\");  \nlist.add(\"string2\");  \n//some other list.add() code......  \nlist.add(\"stringN\");\n```\n1. 使用Arrrays.asList()方法  \n\n``` java\nArrayList\u003cString\u003e list = new ArrayList(Arrays.asList(\"Tom\", \"Jerry\", \"Mike\"));  \n```\n1. 使用双括号\n\n``` java\nList list = new ArrayList\u003cString\u003e(){{  \n    add(\"A\");  \n    add(\"B\");  \n}};\n```\n## HashMap初始化\n### 非static HashMap\n1. 最简单直观的  \n\n``` java\n        Map\u003cString, Integer\u003e  myMap = new HashMap\u003cString, Integer\u003e();\n        myMap.put(\"Li Lei\", 1);\n        myMap.put(\"Han Meimei\", 2);\n```\n\n但是初始化数据多的时候,这样写会很麻烦,有没有更好的办法呢?  \n1. 双括号,匿名内部类初始化HashMap  \n\n``` java\n    Map\u003cString, Integer\u003e myMap = new HashMap\u003cString, Integer\u003e() {\n            {\n                put(\"key1\",\"value1\");\n                put(\"key2\",\"value2\");\n            }\n        };\n```\n### 初始化immutable HashMap\n1. 同上  \n2. 双括号  \n\n``` java\npublic static final HashMap hm = new HashMap(){\n{\n   put(\"key1\",\"value1\");\n   put(\"key2\",\"value2\");\n}\n};  \n```\n\n上面的代码包含了两个隐藏的东西:\n    \\* 一个匿名内部类\n    \\* 一个实例块  \n\n可以对上面的代码进行拆解\n- 拆除匿名内部类  \n\n``` java\nprivate static class MyHashMap() extends HashMap\n{\n    {\n       put(\"key1\",\"value1\");\n       put(\"key2\",\"value2\");\n    }\n}\npublic static final HashMap hm = new MyHashMap();\n```\n- 拆除代码块\n\n``` java\nprivate static class MyHashMap() extends HashMap\n{\n    public MyHashMap()\n    {\n       this.put(\"key1\",\"value1\");  // \u003c-- added 'this'\n       this.put(\"key2\",\"value2\");  // \u003c-- added 'this'\n    }\n}\npublic static final HashMap hm = new MyHashMap();  \n```\n#### immutable HashMap初始化推荐的方法:\n\n``` java\npublic class Test {\n    private static final Map\u003cInteger, String\u003e MY_MAP = createMap();\n\n    private static Map\u003cInteger, String\u003e createMap() {\n        Map\u003cInteger, String\u003e result = new HashMap\u003cInteger, String\u003e();\n        result.put(1, \"one\");\n        result.put(2, \"two\");\n        return Collections.unmodifiableMap(result);\n    }\n}\n```\n\n或者是用Google提供的[Guava](https://github.com/google/guava)  \n\n``` java\nstatic final Map\u003cInteger, String\u003e MY_MAP = ImmutableMap.of(\n    1, \"one\",\n    2, \"two\"\n);\n```\n\n---\n\n**双括号的方式不推荐使用**  \n原因是:\n1. 匿名内部类的声明方式,所以引用中持有着外部类的引用。所以当时串行化这个集合时外部类也会被不知不觉的串行化,当外部类没有实现serialize接口时,就会报错。  \n2. 这种方式其实是声明了一个继承自HashMap的子类。然而有些串行化方法,例如要通过Gson串行化为json,或者要串行化为xml时,类库中提供的方式,是无法串行化Hashset或者HashMap的子类的,从而导致串行化失败。  \n#### 参考链接\n\n**使用双括号初始化可能出现的问题**  \n1. http://www.c2.com/cgi/wiki?DoubleBraceInitialization\n2. http://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way  \n\n**具体的例子**  \n1. [HashMap Construction with initial values](https://coderanch.com/t/386333/java/java/HashMap-Construction-initial-values)\n2. [聊聊 Java 中 HashMap 初始化的另一种方式](https://my.oschina.net/leejun2005/blog/282783)  \n3. [Java的大括号语法糖](https://my.oschina.net/trydofor/blog/79222)\n","author":{"url":"https://github.com/Sitrone","@type":"Person","name":"Sitrone"},"datePublished":"2016-09-22T11:36:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/2/LeetcodeInJava/issues/2"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1d10619b-56d3-cd1e-4f8a-374f921429cd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE9B2:C0A64:75B050:9F242A:6A6006A2
html-safe-noncecb32b561726396b38b5d736e28a07aa850e30abb234a8e04a33172d257b93bcd
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUIyOkMwQTY0Ojc1QjA1MDo5RjI0MkE6NkE2MDA2QTIiLCJ2aXNpdG9yX2lkIjoiMzIyMDY2OTk3NTU5OTUxNTI5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac8d6999e2c9d2852a441d77a67c8edfa248079ac98eb75bcf5c7ed59e6def1f22
hovercard-subject-tagissue:178586357
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Sitrone/LeetcodeInJava/2/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4b3a80a933b0f8708bf663a19c791b367010cdcd52727367a156fb5aefbceeb2/Sitrone/LeetcodeInJava/issues/2
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4b3a80a933b0f8708bf663a19c791b367010cdcd52727367a156fb5aefbceeb2/Sitrone/LeetcodeInJava/issues/2
og:image:altArrayList初始化方法 常规方法 List list = new ArrayList(); list.add("string1"); list.add("string2"); //some other list.add() code...... list.add("stringN"); 使用Arrrays.asList()方法 Arra...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameSitrone
hostnamegithub.com
expected-hostnamegithub.com
None5789899e92b20db289de946d86eb20bf5c9626276695d68f58a8c47cdda699b7
turbo-cache-controlno-preview
go-importgithub.com/Sitrone/LeetcodeInJava git https://github.com/Sitrone/LeetcodeInJava.git
octolytics-dimension-user_id5569320
octolytics-dimension-user_loginSitrone
octolytics-dimension-repository_id61412709
octolytics-dimension-repository_nwoSitrone/LeetcodeInJava
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id61412709
octolytics-dimension-repository_network_root_nwoSitrone/LeetcodeInJava
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release6ddc048ddf80664b9b33547b619db10313c482f1
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/Sitrone/LeetcodeInJava/issues/2#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FSitrone%2FLeetcodeInJava%2Fissues%2F2
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FSitrone%2FLeetcodeInJava%2Fissues%2F2
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=Sitrone%2FLeetcodeInJava
Reloadhttps://github.com/Sitrone/LeetcodeInJava/issues/2
Reloadhttps://github.com/Sitrone/LeetcodeInJava/issues/2
Reloadhttps://github.com/Sitrone/LeetcodeInJava/issues/2
Sitrone https://github.com/Sitrone
LeetcodeInJavahttps://github.com/Sitrone/LeetcodeInJava
Notifications https://github.com/login?return_to=%2FSitrone%2FLeetcodeInJava
Fork 0 https://github.com/login?return_to=%2FSitrone%2FLeetcodeInJava
Star 3 https://github.com/login?return_to=%2FSitrone%2FLeetcodeInJava
Code https://github.com/Sitrone/LeetcodeInJava
Issues 6 https://github.com/Sitrone/LeetcodeInJava/issues
Pull requests 0 https://github.com/Sitrone/LeetcodeInJava/pulls
Actions https://github.com/Sitrone/LeetcodeInJava/actions
Projects https://github.com/Sitrone/LeetcodeInJava/projects
Wiki https://github.com/Sitrone/LeetcodeInJava/wiki
Security and quality 0 https://github.com/Sitrone/LeetcodeInJava/security
Insights https://github.com/Sitrone/LeetcodeInJava/pulse
Code https://github.com/Sitrone/LeetcodeInJava
Issues https://github.com/Sitrone/LeetcodeInJava/issues
Pull requests https://github.com/Sitrone/LeetcodeInJava/pulls
Actions https://github.com/Sitrone/LeetcodeInJava/actions
Projects https://github.com/Sitrone/LeetcodeInJava/projects
Wiki https://github.com/Sitrone/LeetcodeInJava/wiki
Security and quality https://github.com/Sitrone/LeetcodeInJava/security
Insights https://github.com/Sitrone/LeetcodeInJava/pulse
HashMap的几种初始化方法https://github.com/Sitrone/LeetcodeInJava/issues/2#top
https://github.com/Sitrone
Sitronehttps://github.com/Sitrone
on Sep 22, 2016https://github.com/Sitrone/LeetcodeInJava/issues/2#issue-178586357
Guavahttps://github.com/google/guava
http://www.c2.com/cgi/wiki?DoubleBraceInitializationhttp://www.c2.com/cgi/wiki?DoubleBraceInitialization
http://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-wayhttp://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way
HashMap Construction with initial valueshttps://coderanch.com/t/386333/java/java/HashMap-Construction-initial-values
聊聊 Java 中 HashMap 初始化的另一种方式https://my.oschina.net/leejun2005/blog/282783
Java的大括号语法糖https://my.oschina.net/trydofor/blog/79222
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.