The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

HashMap query

Posted by Muruganandam on November 12, 2000 at 1:25 AM

I tested the following code and found out that the HashMap is printing a perfect REVERSE ORDER of elements. Why is it so as documentation maintains that the order will not necessarily be the way which is input.

import java.util.*;

class PrintData {
static void print(Iterator e) {
while(e.hasNext())
System.out.println(e.next());
}
}

class IteratorDemo {
public static void main(String[] args) {
ArrayList v = new ArrayList();
for(int i = 0; i < 5; i++)
v.add(new Integer(i));
HashMap m = new HashMap();
for(int i = 0; i < 15; i++)
m.put(new Integer(i), new Integer(i+1));

TreeMap t = new TreeMap();
for(int i = 0; i < 5; i++)
t.put(new Integer(i), new Integer(i+1));

System.out.println("ArrayList");
PrintData.print(v.iterator());
System.out.println("HashMap");
PrintData.print(m.entrySet().iterator());
System.out.println("TreeMap");
PrintData.print(t.entrySet().iterator());

}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us