Java Maps Inline Initialization

I found a blog sometime back at
http://nileshbansal.blogspot.com/2009/04/initializing-java-maps-inline.html

It shows how to inline initialize a Java map, like so:
Map map = new HashMap<String, String>() {{
put("Harry", "Potter");
put("Ron", "Weasley");
put("Hermione", "Granger");
}};

I find myself forgetting the exact syntax if I haven't used in awhile so I thought I would blog it :-)

An alternate way is using Google collections:
Map<String, String> map = ImmutableMap.of( "x", "y" );

Comments :

0 comments to “Java Maps Inline Initialization”

Post a Comment