简体   繁体   中英

Iteration Order of HashMap key same or not

I want to know that HashMap is give same sequence of key when iterated each time after adding records.

I am using following code

HashMap<String,String> mapObj=new HashMap<String,String>();
mapObj.put("a", "aValue");
mapObj.put("b", "bValue");
mapObj.put("c", "cValue");

for(String key:mapObj.keySet()){
    System.out.println(key+" :: "+mapObj.get(key));
}

for(String key:mapObj.keySet()){
    System.out.println(key+" :: "+mapObj.get(key));
}

output of following program is

b :: bValue  
c :: cValue  
a :: aValue  

b :: bValue  
c :: cValue  
a :: aValue  

If you don't make any changes to the HashMap between the two iterations, you'll likely see the same iteration order (even though it's not guaranteed), since this is a deterministic data structure. However, adding or removing entries between the two iterations will probably change the iteration order.

If you want to rely on the iteration order, use LinkedHashMap, in which (by default) the keys are iterated in the order they were first added to the Map.

If you want to iterate over the keys in some specific order, you can use TreeMap instead (where the keys are ordered according to their natural ordering or the supplied Comparator).

Hash map accept the object to be stored as an argument and generate a number that is unique to it.

HashMap uses hashing to store the entries in hashmap, so there is no gurantee those will appear in specific order. If you want your entries from your HashMap ordered, then you will have to sort it or you can use Treemap

HashMap doesn't maintain the order. If you want your elements to be retrieved in order then better to use LinkedHashMap .

Generally it would be little surprising if the iteration order changed for multiple subsequent invocations (assuming the map itself did not change in between). BUT you should not rely on it as API does not make any guarantee for that.

As per doc :

The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. 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.

You can use LinkedHashMap as its entrySet maintain insertion ordering, as per Java Doc :

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).

TreeMap maintain the natural ordering of keys.

A Red-Black tree based NavigableMap implementation. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM