How to use an instance initializer with a generic HashMap?
问题 Can you use an instance initializer with a generic HashMap? I found this code online, but am having trouble converting it to a generic HashMap instead of a basic HashMap: someMethodThatTakesAHashMap(new HashMap(){{put("a","value-a"); put("c","value-c");}}); 回答1: Here's how: class Foo { static void someMethodThatTakesAHashMap(HashMap<String, String> map) { System.out.println(map); } public static void main(String[] args) { someMethodThatTakesAHashMap(new HashMap<String, String>(){{put("a",