[java] Sorting a Hashtable

Started by
8 comments, last by Neophyte 23 years, 3 months ago
Is there any way to sort a hashtable? I don''t necessarily need to sort the actual Hashtable, but I need some way of getting a sorted list of the elements in the Hashtable. Any help would be greatly appreciated. Neophyte - Death awaits you all with nasty, big, pointy teeth. -
Advertisement
Look up the documentation for the correct method names but i believe hashtable has a method like "hashKeys()" or "keySet()" that will return a set of all the keys of your hash table. you can then iterate through the set. not sure if im answering your question.
You can use my sorttool package. It will only sort Object arrays for compatibility sake, but you can get an Object array from a hashtable like so:

Object [] list = hash.getValues().toArray();

http://www.tapinternet.com/~mark/projects/sorttool.zip
wikidsmot:
The problem is that Hashtable doesn''t guarantee that the Set returned by keySet() is in any particular order.

Jim_Ross:
That code looks pretty good, thanks.
But it''s probably overkill for me to use code that is that generic, so is it OK for you if I tweak it to fit my own purposes and pass it of as my own? (Just kidding, of course. The question (in all seriousness) is if you mind if I use your code as a base from which to make my own implementation. This *is* for use in a commercial product, by the way).


Neophyte


- Death awaits you all with nasty, big, pointy teeth. -
Assuming your keys were of class Key, which implements Comparable, just do something simple like this:

Key [] sortedKeys = (Key [])Arrays.sort(htable.keySet().toArray()); 


Arrays also has a sort(Object [], java.util.Comparator) method, if your key objects don't implement Comparable.

Edited by - c_wraith on January 4, 2001 5:42:32 AM
you could use a treemap so that it is always sorted
quote:Original post by Anonymous Poster

you could use a treemap so that it is always sorted


I agree with this one. If you need to do sorting then it might be the wrong datastructure you have chosen. For frequent sorting an orderedmap/treemap is a better solution - off coarse this depends o how often you do a sorting compared to inserting and looking up in the table.

Always choose the right dfata structure for the job. No data structure can do it all.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
BTW,

If you use many kinds of containers and standard algorithms I can highly recommend the free JGL library from objectspace.

Check it out at

www.objectspace.com

I use it all the time.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Unfortunately, due to the nature of the application I am making, I don''t really have much choice but to use a Hashtable (I am building and manipulating a large DOM-tree, and need to be able to quickly locate a particular element in it from a string).

In the end, though, I have now decided not to sort the hashtable, but rather keep a sorted Vector of the keys, and go through the vector to find which key in the Hashtable to get (not a very neat solution, I know, but it works.).
In effect, something like this:

  Hashtable elements;Vector sorted;/* Fill in the Hashtable and Vector */// To get the 4th element (sorted):String key = (String) sorted.elementAt(4);Element elem = (Element) elements.get(key);  



As the Vector is created sorted (it is created from a database-call), it is easy to keep it sorted when adding and removing elements.

Anyway, thanks for you help all.

Neophyte

- Death awaits you all with nasty, big, pointy teeth. -
quote:Original post by Neophyte
...

As the Vector is created sorted (it is created from a database-call), it is easy to keep it sorted when adding and removing elements.


Haha, ''order by'' in SQL? Awesome, I love easy-ass answers to problems. I don''t mind if you play with the code, just send me back what you have done to it. That goes for anybody.

This topic is closed to new replies.

Advertisement