amicngh
amicngh

Reputation: 7899

Java Set ordering

We all know that Set(Except their such implementation) doesn't guarantee of iteration ordering .So i tried to make sure this with below sample code .

public static void main(String[] args) throws InterruptedException {
    Map<String,String> lMap=new HashMap<String, String>();
    lMap.put("A", "A");
    lMap.put("B", "B");
    lMap.put("C", "C");
    lMap.put("D", "D");
    lMap.put("E", "E");
    lMap.put("F", "F");
    lMap.put("G", "G");
    lMap.put("H", "H");
    lMap.put("I", "I");
    lMap.put("J", "J");
    lMap.put("K", "K");
    lMap.put("L", "L");
    for(int i=0;i<10000;i++){

        Thread.sleep(100);
        Set<Entry<String, String>> entrYset=lMap.entrySet();
        for(Map.Entry<String, String> e:entrYset){
            System.out.println(e.getKey()+" , "+e.getValue());
        }
                  System.out.println("******************************************************");
    }
}

I executed above code many times and found that it is printing records in order.

My question is if java claims HashMap is unordered then why this records are printing in order . If someone can give me reason with example that would be great.

Upvotes: 1

Views: 259

Answers (6)

Kuba
Kuba

Reputation: 3076

You're confusing "is not guaranteed" with "is guaranteed not to be".

Of course HashMap can have some internal ordering.

The only thing the documentation claim is that you should not rely on order of items in a HashMap, and if you do (based on the result of experiment similar to yours) - well, you have been warned.

Upvotes: 0

Sujay
Sujay

Reputation: 6783

You're incorrect. If you look at the Javadoc, it says

This class makes no guarantees as to the order of the map; 
In particular, it does not guarantee that the order will remain constant over time

Note that the document says that no specific guarantee regarding maintaining insertions order can be guaranteed in a HashMap. This in most cases has to do with when the HashMap internally has be resized.

Try playing with the initialCapacity and loadFactor by using this constructor for HashMap(int initialCapacity, float loadFactor) to replicate internal resizing. I think you can see the difference. Oh and when I executed your code, my ordering was totally different from what you posted as part of your question.

Upvotes: 0

erencan
erencan

Reputation: 3763

Hash Tables uses a hash function to put values into set. Therefore its order may change according to hash values. So, you add same values to a hash table with same hash function. That is why you see a order in your set. Try to change a value or order of inserting values. It may change the order of result.

Upvotes: 0

Alpedar
Alpedar

Reputation: 1344

Its because String hash code

public int hashCode() {
    int h = hash;
    if (h == 0 && count > 0) {
        int off = offset;
        char val[] = value;
        int len = count;

        for (int i = 0; i < len; i++) {
            h = 31*h + val[off++];
        }
        hash = h;
    }
    return h;
}

with one letter string it will be h = 31*0+numeric value of character thus all hash codes are 1) low 2) in same order as letters. therefore it is likely that they will be returned in this order.

Upvotes: 1

Jeff Foster
Jeff Foster

Reputation: 44736

The order is the same each time because the hash code of string doesn't change and you are inserting the in the same order. Hashmap is deterministic, so if you create the same hashmap and insert things in the same order, you'll always get a consistent ordering.

Hashmap doesn't give any guarantee that this ordering will remain consistent. If you insert more items the the ordering may completely change as the hash table is rebuilt.

Upvotes: 5

Dominik Sandjaja
Dominik Sandjaja

Reputation: 6476

A re-ordering usually occurs, when you add new elements to the map. If the map gets resized, the order may change.

Upvotes: 2

Related Questions