Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of java.util.Map using insertion order as order

Tags:

java

map

The JDK documentation on java.util.Map states

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

And the documentation on TreeMap states:

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Is there any JDK implementation of Map that uses the order that elements were inserted into the map as the order?

like image 808
Paul Reiners Avatar asked Aug 11 '10 19:08

Paul Reiners


1 Answers

java.util.LinkedHashMap:

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). Note that insertion order is not affected if a key is re-inserted into the map.

like image 191
Andrzej Doyle Avatar answered Oct 14 '22 05:10

Andrzej Doyle