• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Retrieve values from Hashtable in the order in which it has been entered?  RSS feed

 
Kumar Navin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!,
How do I Retrieve values from Hashtable in the order in which it has been entered? My code reads as such...

public static void main(String args[]) {
Hashtable hs = new Hashtable();

hs.put("1716","S M Gupta");
hs.put("2263","Sujit Sinha");
hs.put("1299","Boss");
hs.put("2441","Praveen Kumar");
hs.put("2890","Akshay Joshi");
hs.put("2639","Kamesh Rastogi");
hs.put("2909","Praveen Paliwal");

Enumeration en = hs.keys();
int i=0;

while ( en.hasMoreElements() )
{
String str = (String)en.nextElement();
System.out.println("Code : "+ str + " Name : "+ hs.get(str)+" Hash Code : "+hs.hashCode());

} ///End of while

} //End of main

The output of the execution of the above is as below:
Code : 2909 Name : Praveen Paliwal Hash Code : 967793953
Code : 2263 Name : Sujit Sinha Hash Code : 967793953
Code : 2441 Name : Praveen Kumar Hash Code : 967793953
Code : 1299 Name : Boss Hash Code : 967793953
Code : 1716 Name : S M Gupta Hash Code : 967793953
Code : 2639 Name : Kamesh Rastogi Hash Code : 967793953
Code : 2890 Name : Akshay Joshi Hash Code : 967793953

Thanks in advance.
-Navin.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use LinkedHashMap instead of Hashtable.
You can always make a Hashtable out of a LinkedHashMap by using the appropriate constructor.
[ November 07, 2004: Message edited by: Barry Gaunt ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!