order hashtable as array
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   raffe_paffe
Posted On:   Tuesday, July 17, 2001 07:54 AM

Do you know of a collection (or other class) that has he same functionallity as hashtable (key - value pair) but is sorted like an array(in order you put it in stuff, so to say)?

Re: order hashtable as array

Posted By:   Luke_Biewald  
Posted On:   Friday, July 20, 2001 06:08 PM

The most efficient way if you need to sort the table more than once is to maintain a Hashtable and a Vector of keys.

Re: order hashtable as array

Posted By:   raffe_paffe  
Posted On:   Wednesday, July 18, 2001 12:40 AM

My coworker emily suggested this:


java.util.SortedMap myMap = new

java.util.TreeMap(new

java.util.Comparator() {

public int compare(Object o1, Object o2) {

return 1;

}

public boolean equals(Object o1, Object o2) {

return o1.equals(o2);

}

}

);
Any thoughts?

Re: order hashtable as array

Posted By:   John_Zukowski  
Posted On:   Tuesday, July 17, 2001 10:27 AM

I think what you are after is the LinkedHashMap / LinkedHashSet classes, new to the Java 1.4 release. This maintains insertion order.


If all you want to do is sort them based on some key, use a TreeMap.

Re: order hashtable as array

Posted By:   Naveed_Azhar  
Posted On:   Tuesday, July 17, 2001 08:20 AM

Hello there!!


I believe you can sort the Hashtable. Use the following sample code from me to sort and print the Hashtable


public Hashtable htobject = new Hashtable();
............
............

SortedSet sortedKeys = new TreeSet(htobject.keySet());
Iterator it = sortedKeys.iterator();
while ( it.hasNext() ) {
NaveedClass temp = (NaveedClass)htobject.get((String)it.next());
System.out.println(temp.toString());
}



(Replace NaveedClass with your class )


Naveed Azhar

naveed@hotmail.com
About | Sitemap | Contact