LinkedHashMap ordering

Viewed 5524

As specified in the javadoc for LinkedHashMap, insertion order is not affected if a key is re-inserted into the map but While running the below program,I notice that inserting the same key again in changing the access order.

Map<Integer, String> map = new LinkedHashMap<Integer,String>(16, .75f, true);
    map.put(new Integer(1), "Ajay");
    map.put(new Integer(2), "Vijay");
    map.put(new Integer(3), "Kiran");
    map.put(new Integer(4), "Faiz");

    for(String value:map.values()){
        System.out.println(value);
    }

    String val =map.get(new Integer(3));
    map.put(new Integer(2), "Ravi");
    System.out.println("After changes...");
    for(String value:map.values()){
        System.out.println(value);
    }

On Running the above program i am getting the o/p as follows:

Ajay
Vijay
Kiran
Faiz
After changes...
Ajay
Faiz
Kiran
Ravi

when i reinsert the key 2 using, why it's access order is changed.

Please help me to understand the o/p.

Thanks,

1 Answers
Related