Java Map.of() vs Map.ofEntries() Method


In Java, the Map.of() and Map.ofEntries() static factory methods were introduced in Java 9 as part of the java.util.Map interface to create immutable maps with a concise syntax. These methods are useful when you need to create small, fixed-size maps with a known set of key-value pairs.

1. Map.of()

The Map.of() method allows you to create a map with up to 10 key-value pairs. It has a straightforward syntax and is convenient for creating small maps with a known set of elements.

Map.of() Method Signature: This method allows you to create a map with up to 10 key-value pairs.

static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)

Here’s an example:

MapOfExample.java
package org.websparrow;

import java.util.Map;

public class MapOfExample {

    public static void main(String[] args) {
        Map<String, Integer> map = Map.of("One", 1, "Two", 2, "Three", 3);

        // Accessing elements
        System.out.println("Map: " + map);
        System.out.println("Value of Two: " + map.get("Two"));
    }
}

Output:

console.log
Map: {Three=3, Two=2, One=1}
Value of Two: 2

2. Map.ofEntries()

The Map.ofEntries() method allows you to create maps with more than 10 key-value pairs. It takes Map.Entry instances as parameters, making it suitable for larger maps.

Map.ofEntries() Method Signature: This method allows you to create a map with an arbitrary number of key-value pairs by passing Map.Entry instances.

static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K, ? extends V>... entries)

The Map.Entry interface is defined as follows:

public interface Map.Entry<K,V> {
    K getKey();
    V getValue();
}

The Map.ofEntries() method takes a variable number of Map.Entry instances, allowing for the creation of maps with more than 10 key-value pairs. Each Map.Entry instance represents a key-value pair in the map.

Here’s an example:

MapOfEntriesExample.java
package org.websparrow;

import java.util.Map;
import java.util.Map.Entry;

public class MapOfEntriesExample {

    public static void main(String[] args) {
        Map<String, Integer> map = Map.ofEntries(
                entry("One", 1),
                entry("Two", 2),
                entry("Three", 3),
                entry("Four", 4)
        );

        // Accessing elements
        System.out.println("Map: " + map);
        System.out.println("Value of Three: " + map.get("Three"));
    }

    private static <K, V> Entry<K, V> entry(K key, V value) {
        return Map.entry(key, value);
    }
}
console.log
Map: {Three=3, Four=4, Two=2, One=1}
Value of Three: 3

In the MapOfEntriesExample, the Map.entry() method is used to create Map.Entry instances. The Map.ofEntries() method then takes these entries as parameters to construct the map.

3. Summary

Both Map.of() and Map.ofEntries() methods create immutable maps, meaning that you cannot add, remove, or modify elements once the map is created. If you attempt to do so, an UnsupportedOperationException will be thrown.

4. References

  1. Map- Java Doc
  2. Java 9- Creating Collection using Factory Method of(…)

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.