File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Sorting a HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting a HashTable" Watch "Sorting a HashTable" New topic
Author

Sorting a HashTable

Frank E Robinson
Greenhorn

Joined: Nov 22, 2003
Posts: 2
Hello All,
Would you give me some insight on how to sort values in a has table. I just started teaching myself Java. Below is my code that creates a has with some values. If you could point me to some good references that would be great also.
thanks in advance
import java.util.*;
public class hs
{
static HashSet myHS = new HashSet();

public static void main(String[] arg)
{

createHS();
showHS();
}
public static void createHS()
{
String s1="B", s2="E", s3="U", s4="A", s5="Z", s6="T";
myHS.add(s1);
myHS.add(s2);
myHS.add(s3);
myHS.add(s4);
myHS.add(s5);
myHS.add(s6);
}

public static void showHS()
{
Iterator i = myHS.iterator();
while( i.hasNext() )
{
System.out.println(" tool: " + i.next() );
}
}
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You really can't sort the entries in a Hashtable, HashMap or HashSet. They're not stored in sorted order when you put them into the map, and you can't reorder them later. If you want to sort something, and you want to simultaneously maintain a Map relationship between keys and values, then you want to use a SortedMap - an interface which has one standard implementation, TreeMap. That's the class you want.


"I'm not back." - Bill Harding, Twister
Frank E Robinson
Greenhorn

Joined: Nov 22, 2003
Posts: 2
Thanks Jim, I'll look into SortedMap.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting a HashTable
 
Similar Threads
nested class Danchisholm ques.
Strings
Problem with String
nested class Danchisholm ques.
Strings, StringBuffer and equals()