3

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  
Sunil
  • 437
  • 3
  • 11
  • possibly duplicate of http://stackoverflow.com/questions/2144776/order-of-values-retrieved-from-a-hashmap – Vishnu Jul 09 '15 at 08:45
  • 1
    There is no guarantee at all that the iteration order of any `Map` implementation is consistent; `HashMap` in particular makes no such guarantee. And this is understandable since the keys of a `Map` are a `Set`, which offers no iteration guarantees either. – fge Jul 09 '15 at 09:24

4 Answers4

4

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

Eran
  • 387,369
  • 54
  • 702
  • 768
1

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

kirti
  • 4,499
  • 4
  • 31
  • 60
0

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

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

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

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95