Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve order of insertion in Map.of factory?

Java 9 offers Map.of() feature to easily create a map with fixed values.

Problem: I want to create a map that preserves order of insertion like LinkedHashMap. Is that possible with that factory? At least map.of() does not preserv the order...

like image 980
membersound Avatar asked Sep 10 '18 09:09

membersound


People also ask

How do you preserve an insertion order on a map?

This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.

Do maps preserve order?

” HashMap does not preserve insertion order “. HashMap is collection of Key and Value but HashMap does not give guaranty that insertion order will preserve.

Does immutable map preserve order?

util. Map. of() , it does not preserve ordering.

Does map Entryset preserve order?

According to the Javadocs, yes. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).


2 Answers

As documented on the Java apidoc of Map (emphasis mine):

Unmodifiable Maps

The Map.of, Map.ofEntries, and Map.copyOf static factory methods provide a convenient way to create unmodifiable maps. The Map instances created by these methods have the following characteristics:

  • ...
  • The iteration order of mappings is unspecified and is subject to change.
  • ...

Unfortunately, there is no equivalent convenience method in the Java API that creates a LinkedHashMap. If you want a consistent iteration order, then you will need to manually create a LinkedHashMap and populate it (and - if needed - wrap it using Collections.unmodifiableMap).

Consider creating your own convenience method that does the equivalent of Map.of but with a consistent iteration order (or find an existing library that already provides this).

like image 171
Mark Rotteveel Avatar answered Sep 29 '22 03:09

Mark Rotteveel


There isn't a factory method like LinkedHashMap::of indeed, and a Map does not have an order per-se, so the only way I see it is to build a LinkedHashMap if you really needed one.

Btw from the JEP itself:

Static factory methods on concrete collection classes (e.g., ArrayList, HashSet) have been removed from this proposal ...

There is another wrinkle, which is that static methods on classes are inherited by subclasses. Suppose a static factory method HashMap.of() were to be added. Since LinkedHashMap is a subclass of HashMap, it would be possible for application code to call LinkedHashMap.of(). This would end up calling HashMap.of(), not at all what one would expect!

Point here is that static methods are inherited, but not overridable, thus if such a method would have been added to HashMap it could have not been overridden in LinkedHashMap.

If you can use guava, you could use ImmutableMap that is documented as:

An immutable, hash-based Map with reliable user-specified iteration order...

like image 33
Eugene Avatar answered Sep 29 '22 01:09

Eugene