Java Language Map Factory Method Examples


Example

  • Map<Integer, Integer> immutableEmptyMap = Map.of();
    • Initializes an empty, immutable Map<Integer, Integer>.
  • Map<Integer, Integer> immutableMap = Map.of(1, 2, 3, 4);
    • Initializes an immutable Map<Integer, Integer> with two initial key-value entries.
  • Map<Integer, Integer> immutableMap = Map.ofEntries(Map.entry(1, 2), Map.entry(3, 4));
    • Initializes an immutable Map<Integer, Integer> with two initial key-value entries.
  • Map<Integer, Integer> mutableMap = new HashMap<>(immutableMap);
    • Initializes a mutable Map<Integer, Integer> from an immutable Map<Integer, Integer>.