Is there a more streamlined way to do the following?
Map map = new HashMap();
map.put(\"a\", \"apple\");
map.put(
Java 9 adds Map.of
, such as:
Map map = Map.of("a", "apple", "b", "bear", "c", "cat");
Up to 10 entries are supported. For more entries you can use the overload taking Entry:
Map map
= Map.ofEntries
(Map.entry("a", "apple")
, Map.entry("b", "bear")
, Map.entry("c", "cat"));
Note that these methods do not return a HashMap. It returns an optimized immutable map.