SyntaxHighlighter JS

2013-02-21

Initialize Map

Sometimes code is more clear by initializing a Map at its declaration. For example,

Map<String,String> map = new HashMap<String,String>() {{
    put("Key 1", "Value 1");
    put("Key 2", "Value 2");
}};

Behind the Java scenes, this is a combination of an anonymous inner class (the first outer set of brackets) and an instance initializer block (the second inner set of brackets).

No comments:

Post a Comment