This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Hash Table - Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hash Table - Sorting" Watch "Hash Table - Sorting" New topic
Author

Hash Table - Sorting

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Hi

I had created a HashTable instance with entries in it. I know HashTable class doesn't support sorting. But I need the key/value to be sorted in someway. TreeMap class supports sorting. But I don't know how to use it. Please help to sort the entries.



Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

TreeMaps sort entries by key, based on the "natural order" of the key. What that means is that whatever object you use as a key must implement the Comparable interface. This is an interface which defines the compareTo(Object o) method, so the implementing object will have to implement how you decide one object needs order after another.

The easiest way to use a TreeMap is to use a Key which already implements Comparable - Strings, Integers, Longs etc. are the most obvious. Assuming you do this, you know that as you put object into the TreeMap, they are ordered as defined by the key's natural order.
[ March 01, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
Don't get me started about those stupid light bulbs.
 
subject: Hash Table - Sorting
 
Similar Threads
Hashcode for Long
what is the replacement for BatchEnvironment ?
using a protected method
Hash Table
what is the replacement for BatchEnvironment ?