Wednesday, June 29, 2005

HashMap maladies...

been working with hashmaps for quite a long time now... but one thing escapes me...Consider the following snippet to display the contemts of a HashMap in a sequential manner.
 //temp2 is a hashmap
Set set2=temp2.entrySet();
Iterator iHash2=set2.iterator();

while(iHash2.hasNext()){
Map.Entry me2= (Map.Entry) iHash2.next();
System.out.println(me2.getKey());
}
well if u understand the code ...( i dont see any issues here)..read on
I printed the order in which the elements were entered in the HashMap and later printed the hashMap using the above algorithm... both are different.... Any idea why??

1 comment:

  1. Well am replying to myself after one year...
    I was a moron and did not understand HashMap which is maintained as a balanced red black tree... obviously the ordering will be lost.

    ReplyDelete