• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hash Table - Sorting

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic