From the documentation of Map.java -
The
Map.of()andMap.ofEntries()static factory methods provide a convenient way to create immutable maps.
But when I already can use overloaded method ...
Map.of("k1","v1","k2","v2","k3","v3"...);
... what is the use of Map.ofEntries which also
returns an immutable map containing keys and values extracted from the given entries and the entries themselves are not stored in the map.
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.Entrycontaining the given key and value. These entries are suitable for populating Map instances using theMap.ofEntries()method.The
Entryinstances 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With