xxxxxxxxxx
public static Map<String, String> articleMapOne;
static {
articleMapOne = new HashMap<>();
articleMapOne.put("ar01", "Intro to Map");
articleMapOne.put("ar02", "Some article");
}
xxxxxxxxxx
import java.util.HashMap;
import java.util.Map;
public class MapInitialization {
public static void main(String[] args) {
// Create a new map and initialize it with values
Map<String, Integer> map = new HashMap<String, Integer>() {{
put("key1", 1);
put("key2", 2);
put("key3", 3);
}};
// Print the contents of the map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}