Map<String,String> test =
new HashMap<String, String>{"test":"test","test":"test"};
Is there a way to initialize HashMap like the code above? Is the code above grammatically correct?
You can't use the code above. You have to add them to the elements one by one.
public class Demo
{
private static final Map<String, String> myMap;
static
{
myMap = new HashMap<String, String>();
myMap.put("a", "b");
myMap.put("c", "d");
}
}
© 2024 OneMinuteCode. All rights reserved.