Is the Java Hashmap Keyset() Iteration Order Consistent

is the Java HashMap keySet() iteration order consistent?

If it is not stated to be guaranteed in the API documentation, then you shouldn't depend on it. The behavior might even change from one release of the JDK to the next, even from the same vendor's JDK.

You could easily get the set and then just sort it yourself, right?

Same iteration order on Map.keySet and Map.values?

No, there is no guarantee, although in practice it will happen (there's no good reason for the map to use a different iterator for the keys and values).

If you want to guarantee iteration order, iterate the entrySet():

for (Map.Entry<Integer,Integer> entry : map.entrySet())
// ...

Since you ask about HashMap, note also that any changes to the map will potentially change iteration order, as a result of the mapbeing rehashed.

Is the order of values retrieved from a HashMap the insertion order

The values are printed in the order in which they have been inserted. Is this true in general? I was expecting the values to be printed in random order.

The HashMap API does not define the order of iteration.

However, if you look at the implementation of HashMap, you can deduce that there is a complex transient relationship between the iteration order, the keys' hash values, the order in which the keys were inserted and the size of the hashtable. This relationship gets scrambled if the hashtable resizes itself.

In your case, you are using Integer keys which means that the hash values of the keys are the key values themselves. Also, you inserted the entries in key order. This leads (fortuitously!) to the iteration order matching the insertion order. But if you kept inserting more keys, you would find that the iteration order "wraps around". Then as the table goes through a series of resizes, the order will get progressively more and more scrambled.

In short, what you are seeing is an artefact of the hashtable implementation, and not something that you can (or should) sensibly make use of. Not least because it could change from one Java release to the next.

Iteration Order of HashMap key same or not

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

Java HashMapInteger, ... keyset() iterating in sorted order

No particular order is guaranteed for the HashMaps and HashSets, period. You may derange your order by removing and adding some elements.

Checkout this:

import java.util.HashMap;

public class test {
public static void main(String[] args) {
HashMap<Integer, String> someMap = new HashMap<Integer, String>();

someMap.put(0, "nothing");
someMap.put(7, "nothing");
someMap.put(3, "nothing");
someMap.put(4, "nothing");
someMap.put(10, "nothing");
someMap.put(11, "nothing");
someMap.put(2, "nothing");
someMap.put(10004, "nothing");
someMap.put(1, "nothing");
someMap.put(5, "nothing");

for (Integer someInt : someMap.keySet()) {
System.out.println(someInt);
}
}
}

In hashsets of most sorts the hashing function rules out where particular value goes.



Related Topics



Leave a reply



Submit