A way to get a sorted enumeration?

G

Guest

I have a Hashtable of IDs and values. Im wondering if there is some way to
get the information out sorted by the 'values' value. A better way to store
this information to make it easier to access?

Probably a stupid question. "Go write a class".
 
S

secret

It's all about the underlying collection you use. You want to use a
SortedMap, the only one of which (that I'm aware of) is the TreeMap. That
should sort you out. Of course you should also stop using an enumberation
and join the world of Java 2 with iterators! :)

Hope that helps.

alan
 
V

VisionSet

I have a Hashtable of IDs and values. Im wondering if there is some way to
get the information out sorted by the 'values' value. A better way to store
this information to make it easier to access?

Probably a stupid question. "Go write a class".

If I understand you correctly, you want to produce a List of the values in
your Hashtable and have them sorted?

Object[] myArray = yourHashtable.values().toArray();
Arrays.sort(myArray); // objects must implement comparable
or
Arrays.sort(myArray, myComparator); // write a class that implements
Comparator

List myList = Arrays.toList(myArray);

I haven't done this, there is just a chance this will mess with the
underlying Map.
If this is the case you will need to copy the collection 1st. There is no
warning of this in the API, so it's probably okay.

On the other had maybe you wanted to sort the Map itself by the values. I
think in that case you will have to roll your own.
 
J

John C. Bollinger

secret said:
It's all about the underlying collection you use. You want to use a
SortedMap, the only one of which (that I'm aware of) is the TreeMap. That
should sort you out. Of course you should also stop using an enumberation
and join the world of Java 2 with iterators! :)

Hope that helps.

alan

A SortedMap is of no help if you want to sort on the values in the Map.
SortedMaps are sorted on the keys.

You will need to put the information in a temporary array or collection
and sort it there. There are several ways to do this, but if you want
to retain the ID and associated value as a unit then you will probably
find it easiest to work with the Map.Entry objects obtained from your
Hashtable's entrySet(). You will need to write a Comparator that
defines the appropriate ordering of the Map.Entry objects, based on
their value objects. You then have three main options:

(1) obtain an array of the map entries by means of the entry set's
toArray() method, and sort that using Arrays.sort(Object[], Comparator);

(2) construct a List populated with the map entries and sort that using
Collections.sort(List, Comparator); or

(3) construct a SortedSet based on your Comparator and containing the
map entries.

If all were equal I'd do (1).


John Bollinger
(e-mail address removed)
 
S

Sudsy

mmm said:
I have a Hashtable of IDs and values. Im wondering if there is some way to
get the information out sorted by the 'values' value. A better way to store
this information to make it easier to access?

Probably a stupid question. "Go write a class".

Hashtable ht = new Hashtable();
// populate ht
ArrayList values = new ArrayList( ht.values() );
Collections.sort( values );
// now do with it what you wish
 
C

Chris Smith

VisionSet said:
Object[] myArray = yourHashtable.values().toArray();
Arrays.sort(myArray); // objects must implement comparable
or
Arrays.sort(myArray, myComparator); // write a class that implements
Comparator

List myList = Arrays.toList(myArray);

I haven't done this, there is just a chance this will mess with the
underlying Map.

No, it won't.
On the other had maybe you wanted to sort the Map itself by the values. I
think in that case you will have to roll your own.

Not necessarily. Composition is your friend. In that case, you could
use existing data structures to accomplish your goal. Specifically, you
would need a composition of the standard map (not necessarily sorted)
and a backward SortedMap of (key = original value) to (value = List of
original keys), and you could implement all of the appropriate Map
methods but provide sorting by value.

That may sound redundant, but you're essentially asking for two lookup
data structures anyway; one for the standard Map.get implementation, and
one for the Set views to get them in the right order.

Whether you do this or sort on-the-fly when you need the sorted values
depends on how frequently you have that need, how time-critical the
operations that require the sorted values are, and how large the
collection is likely to be.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top