Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] Sorting a Hashtable


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Neophyte   Members   -  Reputation: 503

Like
Likes
Like

Posted 02 January 2001 - 10:31 PM

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. -

Sponsor:

#2 wikidsmot   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2001 - 06:19 AM

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.

#3 Jim_Ross   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2001 - 06:48 AM

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

#4 Neophyte   Members   -  Reputation: 503

Like
Likes
Like

Posted 03 January 2001 - 11:38 AM

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. -

#5 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 January 2001 - 10:41 PM

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

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 04 January 2001 - 04:02 AM

you could use a treemap so that it is always sorted

#7 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 January 2001 - 12:47 AM

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


#8 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 January 2001 - 12:50 AM

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

#9 Neophyte   Members   -  Reputation: 503

Like
Likes
Like

Posted 07 January 2001 - 05:12 AM

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. -

#10 Jim_Ross   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 January 2001 - 08:58 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS