Main Tutorials

How to initialize a HashMap in Java

This article shows different ways to initialize HashMap in Java.

  1. Initialize a HashMap (Standard)
  2. Collections.singletonMap
  3. Java 9 Map.of
  4. Java 9 Map.ofEntries
  5. Create a function to return a Map
  6. Static Initializer
  7. Java 8, Stream of SimpleEntry
  8. Conclusion

After initialized a HashMap, the result is either a mutable map or an immutable map:

  • Mutable map – It means we can modify the Map entries.
  • Immutable map – It means we can’t add or modify the Map entries, and if we modify it, it throws UnsupportedOperationException.

1. Initialize a HashMap (Standard)

This example is a standard Java way to declare and initialize a HashMap. The result is a mutable map, and we can use Collections.unmodifiableMap to convert a mutable map to an immutable map.


  // normal way, mutable map
  Map<String, String> map = new HashMap<>();
  map.put("key1", "value1");
  map.put("key2", "value2");
  map.put("key3", "value3");
  System.out.println(map.getClass());             // java.util.HashMap

  // convert mutable Map to immutable map
  Map<String, String> immutableMap = Collections.unmodifiableMap(map);
  System.out.println(immutableMap.getClass());    // java.util.Collections$UnmodifiableMap
  immutableMap.put("key4", "value4");             // UnsupportedOperationException

2. Collections.singletonMap

This example uses Collections.singletonMap to initialize a single entry SingletonMap, an immutable map.


  // @since 1.3
  // single entry immutable map
  Map<String, String> map = Collections.singletonMap("key1", "value1");

  System.out.println(map.getClass());   // java.util.Collections$SingletonMap
  map.put("key2", "value2");            // throws UnsupportedOperationException

3. Java 9 Map.of

Java 9 introduced a new API Map.of returns an unmodifiable or immutable map containing up to 10 entries.


  // Java 9, max is 10 entries or elements, immutable map
  Map<String, String> map = Map.of(
      "key1", "value1",
      "key2", "value2",
      "key3", "value3"
  );

  System.out.println(map.getClass());   // java.util.ImmutableCollections$MapN
  map.put("key3", "value4");            // throws UnsupportedOperationException

Review the Java 9 source code of Map.java. They created 10 Map.of overloading methods to initialize a Map containing up to 10 entries or elements.

Map.java

  static <K, V> Map<K, V> of(K k1, V v1) {
    return new ImmutableCollections.Map1<>(k1, v1);
  }

  static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
    return new ImmutableCollections.MapN<>(k1, v1, k2, v2);
  }

  static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
      return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3);
  }

  // up to 10 elements...

4. Java 9 Map.ofEntries

Java 9 introduced a similar API, Map.ofEntries, which returns an unmodifiable or immutable map, but this Map has no limit of the Map entry, and we can create as many entries as we want.


  // Java 9, no limit of Map entry, immutable map.
  Map<String, String> map = Map.ofEntries(
      Map.entry("key1", "value1"),
      Map.entry("key2", "value2"),
      Map.entry("key3", "value3")
  );

  System.out.println(map.getClass());    // java.util.ImmutableCollections$MapN
  map.put("key4", "value4");             // throws UnsupportedOperationException

5. Create a function to return a Map

We can create a function to initialize a HashMap, which works for all Java versions. The function returns a mutable map.


  // It works for all Java versions, mutable map.
  Map<String, String> map = createMap();

  System.out.println(map.getClass()); // java.util.HashMap
  map.put("key4", "value4");          // yes, we can do it

  private static Map<String, String> createMap() {
      Map<String, String> map = new HashMap<>();
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");
      return map;
  }

6. Static Initializer

This solution is for a static Map variable, and it returns a mutable map.


  public static Map<String, String> map;
  static {
      // mutable map
      map = new HashMap<>();
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");
  }

7. Java 8, Stream of SimpleEntry

This example creates a stream of SimpleEntry and returns a mutable map.

Do not use the below method to initialize a HashMap, I don’t see any benefits of a stream of SimpleEntry, error-prone and ugly syntax, just for reference.


import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toMap;
//...

  // mutable map
  Map<String, String> map = Stream.of(
          new AbstractMap.SimpleEntry<>("key1", "value1"),
          new AbstractMap.SimpleEntry<>("key2", "value2"),
          new AbstractMap.SimpleEntry<>("key3", "value3"))
          .collect(
                  toMap(AbstractMap.SimpleEntry::getKey,
                          AbstractMap.SimpleEntry::getValue)
          );
  System.out.println(map.getClass()); // java.util.HashMap
  map.put("key4", "value4");          // no problem.

Conclusion

Below is a little review of each of the ways to initialize a HashMap.

  1. Initialize a HashMap (Standard) – The best.
  2. Collections.singletonMap – Less use cases.
  3. Java 9 Map.of – If you have Java 9 and want to create an immutable Map.
  4. Java 9 Map.ofEntries – Same as the above.
  5. Create a function to return a Map – It works everywhere.
  6. Static Initializer – For static Map variable.
  7. Java 8, Stream of SimpleEntry – Do not use this, unless you want to show off the Java 8 skill.

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-basic/collections

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Jalpen Shah
3 years ago

very nice work..thank you 🙂

alex
3 years ago
Reply to  Jalpen Shah

yeah he’s really helpful

alex
3 years ago

nice work