java - How to directly initialize a HashMap (in a literal way)? - Stack Overflow
This is one way.
HashMap<String, String > h = new HashMap<String, String>(){{ put("a","b"); }}; However, you should be careful and make sure that you understand the above code (it creates a new class that inherits from HashMap). Therefore, you should read more here: http://www.c2.com/cgi/wiki?DoubleBraceInitialization , or simply use Guava:
Read full article from java - How to directly initialize a HashMap (in a literal way)? - Stack Overflow
No comments:
Post a Comment