Hashmap + Insertion Order

I read about HashMap and it says insertion order is not maintained. I have executed below code and for 10,000 times response is returned in same order.

Also in key, I am just changing prefix from E to M. Can someone please help in explaining this behavior?

for ( int i = 0; i < 10000; i++ ) {
            Map<String, String> map1 = new HashMap<String, String>();
            map1.put( "E._AUTO", "20");
            map1.put( "E._ITERATIVE", "20");
            map1.put( "E._ANDREW", "20");
            System.out.println(map1);

            Map<String, String> map2 = new HashMap<String, String>();
            map2.put( "M._AUTO", "20");
            map2.put( "M._ITERATIVE", "20");
            map2.put( "M._ANDREW", "20");
            System.out.println(map2);
        }

Output:

{E._ANDREW=20, E._ITERATIVE=20, E._AUTO=20}
{M._ITERATIVE=20, M._AUTO=20, M._ANDREW=20}
Jon Skeet
people
quotationmark

I have executed below code and for 10,000 times response is returned in same ordered.

That's just what happens to occur with the version you're using and the values you're inserting. There's no guarantee it will continue to occur, or that insertion order is maintained if you add different values, or if you remove items then add others.

Basically, it's not saying that it definitely won't be in a particular order - it's saying that you absolutely should not rely on it being in that order.

Also note that if you expected insertion order to be maintained, your examples already demonstrate that it's not. The output shows items not being presented in insertion order.

LinkedHashMap will maintain insertion order, by maintaining a linked list of entries alongside the hash map internally. (An exception here is that there's a constructor which allows you to specify that items will be presented in access order rather than insertion order, which is usually used when you want this as the basis of a cache.)

people

See more on this question at Stackoverflow