Any guesses on how would you create a Map of 26 elements?

The primary difference between the two factory methods in Map that you already linked is that :

Map.ofEntries

Returns an immutable map containing keys and values extracted from the given entries (that are not bounded in count)

From the JEP-269:Convenience Factory Methods for Collections:

For larger numbers of entries, an API will be provided that will create a Map instance given an arbitrary number of key-value pairs:

Map.ofEntries(Map.Entry<K,V>...)

While this approach is analogous to the equivalent varargs APIs for List and Set, it unfortunately requires that each key-value pair be boxed. A method for boxing keys and values, suitable for static import, will make this more convenient:

Map.Entry<K,V> entry(K k, V v)

Your assumption about the method .of() from Map is somewhat incorrect probably because while this would compile with Java9:

List<Integer> values = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // note 11 elements here

Set<String> keys = Set.of("z", "o", "tw", "th", "fo", "fi", "si", "se", "e", "n", "te");

This, on the other hand, wouldn't:

Map<String, Integer> map = Map.of("z", 0, "o", 1,
      "tw", 2, "th", 3, "fo", 4, "fi", 5,
      "si", 6, "se", 7, "e", 8, "n", 9, "te", 10); // this would not compile

The reason for that is since there is a varargs implementation for List.of and Set.of but to create a similar API for Map both the keys and values were supposed to be boxed as stated in the JEP as well. So, the same was created using varargs of type Map.entry() as:

Map<String, Integer> map = Map.ofEntries(Map.entry("z",0),
       Map.entry("o",1),Map.entry("t",2)...so on);

Furthermore from the documentation of Map.entry() which is also introduced Since:9 -

Returns an immutable Map.Entry containing the given key and value. These entries are suitable for populating Map instances using the Map.ofEntries() method.

The Entry instances created by this method have the following characteristics:

  • They disallow null keys and values. Attempts to create them using a null key or value result in NullPointerException.

  • They are immutable. Calls to Entry.setValue() on a returned Entry result in UnsupportedOperationException.

  • They are not serializable.

  • They are value-based. Callers should make no assumptions about the identity of the returned instances. This method is free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.

which are similar to the characteristics of Immutable Map Static Factory Methods introduced recently.

Answer from Naman on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ difference between map.ofentries() and map.of()
Difference Between Map.ofEntries() and Map.of() | Baeldung
January 8, 2024 - Take a closer look at these two static factory methods for immutable maps and explain which one is suitable for which purpose.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Map.html
Map (Java Platform SE 8 )
July 21, 2026 - An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_map.asp
Java Map
The Map interface is a part of the Java Collections Framework and is used to store key-value pairs.
๐ŸŒ
Foojay
foojay.io โ€บ home โ€บ java core โ€บ comparing map.of() and new hashmap() in java
Comparing Map.of() and new HashMap() in Java
March 17, 2023 - Map.of() is a method introduced in Java 9, which allows developers to create an immutable map with up to 10 key-value pairs.
Top answer
1 of 3
75

Any guesses on how would you create a Map of 26 elements?

The primary difference between the two factory methods in Map that you already linked is that :

Map.ofEntries

Returns an immutable map containing keys and values extracted from the given entries (that are not bounded in count)

From the JEP-269:Convenience Factory Methods for Collections:

For larger numbers of entries, an API will be provided that will create a Map instance given an arbitrary number of key-value pairs:

Map.ofEntries(Map.Entry<K,V>...)

While this approach is analogous to the equivalent varargs APIs for List and Set, it unfortunately requires that each key-value pair be boxed. A method for boxing keys and values, suitable for static import, will make this more convenient:

Map.Entry<K,V> entry(K k, V v)

Your assumption about the method .of() from Map is somewhat incorrect probably because while this would compile with Java9:

List<Integer> values = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // note 11 elements here

Set<String> keys = Set.of("z", "o", "tw", "th", "fo", "fi", "si", "se", "e", "n", "te");

This, on the other hand, wouldn't:

Map<String, Integer> map = Map.of("z", 0, "o", 1,
      "tw", 2, "th", 3, "fo", 4, "fi", 5,
      "si", 6, "se", 7, "e", 8, "n", 9, "te", 10); // this would not compile

The reason for that is since there is a varargs implementation for List.of and Set.of but to create a similar API for Map both the keys and values were supposed to be boxed as stated in the JEP as well. So, the same was created using varargs of type Map.entry() as:

Map<String, Integer> map = Map.ofEntries(Map.entry("z",0),
       Map.entry("o",1),Map.entry("t",2)...so on);

Furthermore from the documentation of Map.entry() which is also introduced Since:9 -

Returns an immutable Map.Entry containing the given key and value. These entries are suitable for populating Map instances using the Map.ofEntries() method.

The Entry instances created by this method have the following characteristics:

  • They disallow null keys and values. Attempts to create them using a null key or value result in NullPointerException.

  • They are immutable. Calls to Entry.setValue() on a returned Entry result in UnsupportedOperationException.

  • They are not serializable.

  • They are value-based. Callers should make no assumptions about the identity of the returned instances. This method is free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.

which are similar to the characteristics of Immutable Map Static Factory Methods introduced recently.

2 of 3
19

Java 9 introduced creating small unmodifiable Collection instances using a concise one line code, for maps the signature of factory method is:

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

This method is overloaded to have 0 to 10 key-value pairs, e.g.

Map<String, String> map = Map.of("1", "first");
Map<String, String> map = Map.of("1", "first", "2", "second");
Map<String, String> map = Map.of("1", "first", "2", "second", "3", "third");

Similarly you can have up to ten entries.

For a case where we have more than 10 key-value pairs, there is a different method:

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

Here is the usage.

Map<String, String> map = Map.ofEntries(
  new AbstractMap.SimpleEntry<>("1", "first"),
  new AbstractMap.SimpleEntry<>("2", "second"),
  new AbstractMap.SimpleEntry<>("3", "third"));
๐ŸŒ
Redisson
redisson.pro โ€บ glossary โ€บ java-map.html
What is a Java map? | Redisson
Java maps are an implementation of the map abstract data type for the Java programming language. Maps link keys with values: given a key, the map will return the associated value. Each key is unique, but the associated values may not be.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ threadsafe โ€บ java-maps-simplified-understanding-mutable-vs-immutable-implementations-7bd848dde161
Java Maps Simplified: Understanding Mutable vs. Immutable Implementations | by Alireza Alimohammadi | ThreadSafe | Medium
June 29, 2025 - In this article, we will explore how to declare and initialize a map in Java, discuss the differences between immutable and mutable maps, and explain why assigning the result of Map.of() to a HashMap is not permitted, and compare their available methods.
๐ŸŒ
Javacodehouse
javacodehouse.com โ€บ blog โ€บ java-map-of
Creating Immutable Maps with Java
January 24, 2024 - However, there are some differences in terms of flexibility and use cases. ... Map.of(): This method allows you to create a map with up to 10 key-value pairs.
๐ŸŒ
Oracle
docs.oracle.com โ€บ en โ€บ java โ€บ javase โ€บ 21 โ€บ docs โ€บ api โ€บ java.base โ€บ java โ€บ util โ€บ Map.html
Map (Java SE 21 & JDK 21)
January 20, 2026 - An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ initialize-a-static-map-using-java-9-map-of
Initialize a static Map using Java 9 Map.of() - GeeksforGeeks
July 12, 2025 - Pass the map values as Key and Value pair in the Map.of() method. A static factory Map instance is returned.
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ new java 9 map.of() methods
r/java on Reddit: New Java 9 Map.of() methods
March 11, 2016 - That's Java for you, always hiding important information just in case they want to change the implementation later and break all your code. ... Or you could write your code in a way that's resistant to such changes. Need this to be a TreeMap? TreeMap<String, Integer> map = new TreeMap<>(Map.of("Hello", 1, "World", 2)); Now Map.of() can return ThorMap or HawkeyeMap and you just don't care.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ mastering-complex-data-structures-in-java โ€บ lessons โ€บ exploring-maps-and-hashmap-in-java
Exploring Maps and HashMap in Java
In the above code, we create a PhoneBook class that uses a HashMap to store contacts. As you can see, Maps simplify the processes of adding, modifying, and accessing information with unique keys. Java Maps enable a variety of operations for manipulating data, such as adding, retrieving, and deleting key-value pairs, and more.
๐ŸŒ
Chunker
chunker.app
Chunker: Minecraft Java and Bedrock World Converter
Chunker is a free and open-source tool that can convert Minecraft worlds between different versions and platforms (Java and Bedrock). It has a simple GUI and a command line interface.
๐ŸŒ
Medium
medium.com โ€บ @arshikasingh41 โ€บ java-maps-829920cb489f
Java Maps. Introduction to Java Maps | by Arshika Singh | Medium
March 4, 2025 - In the Java programming language, a Map is a part of the Collections Framework that allows you to store key-value pairs. Unlike other collection types like List or Set which store individual elements, a Map stores mappings from keys to values.
๐ŸŒ
Google
google.com โ€บ intl โ€บ en_uk โ€บ streetview
Explore Street View and add your own 360 images to Google Maps.
Learn how to navigate and use Street View. Explore the world without leaving your couch and create and add your own images into Google Maps.
๐ŸŒ
MapStruct
mapstruct.org
MapStruct โ€“ Java bean mappings, the easy way!
Java bean mappings, the easy way! ... MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.
๐ŸŒ
Minecraft Maps
minecraftmaps.com
Minecraft Maps
Update to v4.1, major fixes and repairs made to certain parts of map that weren't ready. Additional minor fixes and touch ups made to v4 as well. Next update to 4.2 to drop shortly. v4 remains on Java 1.21.113d
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-maps-in-java
How To Use Maps in Java | DigitalOcean
Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_hashmap.asp
Java HashMap
Java Examples Java Videos Java ... Java Syllabus Java Study Plan Java Interview Q&A ... A HashMap stores items in key/value pairs, where each key maps to a specific value....