• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to keep the order of elements in hashtable

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
Im adding elements into hashtable but I need that the order of adding them will
keep in the same order in the hashtable , can it be done ?
thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the java.util.LinkedHashMap instead.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Forgot to mention that the LinkedHashmap, unlike Hashtable, is not synchronized. So, if you need the map to be synchronized, don't forget to wrap that LinkedHashmap in a synchronized map from the Collections class.

Henry
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its more complicated then that , you see I have object that extends the hashtable class and I cant
Change this implementation , and this class needs to have the ability to keep the order of the elements that was
Enterd , maybe I can play with some inner methods inside the class that the end result will give me ordered hashtable ?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
Well its more complicated then that , you see I have object that extends the hashtable class and I cant
Change this implementation , and this class needs to have the ability to keep the order of the elements that was
Enterd , maybe I can play with some inner methods inside the class that the end result will give me ordered hashtable ?


Extending a collection class is very often the wrong thing to do. Better to contain, rather than extend, the collection. However, you say you cannot change this.

If so, you can keep an additional List of the keys, in the correct order. It will be up to you to ensure that the List and the Hashtable are kept in sync, whenever something is added or removed.

As already mentioned, Hashtable is synchronized. If you are relying on this synchronisation to give some degree of thread-safety (*), you will need additional synchronisation to ensure thread-safe simultaneous updates to the List. Do not imagine that using Vector as your List will deliver this thread-safety (**).

(*) Hashtable's synchronisation very rarely delivers thread-safety in a real application, because most tables are associated with some other data. No new code should ever use Hashtable; use HashMap instead.

(**) Similar arguments apply to Vector. Never use it in new code - use ArrayList.
[ January 03, 2008: Message edited by: Peter Chase ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks for the reply
I don�t understand how can you keep additional set of keys that will keep the order of the hashtable
cant I get the keys and values into map = new MapTree() and then copy this ordered map into hashtable somehow ?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
cant I get the keys and values into map = new MapTree() and then copy this ordered map into hashtable somehow ?


You mean TreeMap?

If the keys have a natural order (e.g. they're strings and alphabetical order is OK), you can use TreeMap.

However, TreeMap does not store the keys in the order they were added, which is what you previously seemed to want. TreeMap returns keys in their natural order (or an order defined by a Comparator).
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i ment that if i do

i will get the same order i enterd the key/values
this is what i need to keep the order .
but i need it in Hashtable.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fundamentally, you can't control the order of things in the Hashtable itself. You can override all the methods to behave like a LinkedHashMap - but if you can override the methods, why can't you just replace the Hashtable with LinkedHashMap? The latter will be much, much easier. I suppose the next bast thing would be to override all the methods to delegate to a LinkedHashMap. That way people can still pretend that the class "is a" Hashtable (assuming that's necessary for some reason), but in reality all of its behavior will be implemented by the LinkedHashMap:


I didn't use generics since a class that uses Hashtable probably wasn't written this century anyway. But you can modify the code as needed. This is just a rough idea.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.5.0/docs/guide/collections/reference.html

SortedMap & TreeMap are what you're interested in.

If it's a subclass that you can't change - then you can't change it.

You can make a custom iterator that grabs the keys, sorts them, then delivers the items in that order.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Shirley:

SortedMap & TreeMap are what you're interested in.


No, that's not what he wants, as Peter already explained.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and thanks for the fast reply
but i must say im confused ..
can i keep the order of the hashtable some how ?
or it can't be done .
the finel result must be hashtable.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SortedMap/TreeMap posts are irrelevant.

A Hashtable does not retain order. It might be possible, as someone suggested, to override enough of its methods to make it retain the order (e.g. in a List). I reckon you'd end up overriding so much that you'd basically have rewritten it. How that would interact with your existing class that extends Hashtable, I don't know. Personally, I don't rate your chances of success.

It would probably be better to take a step back and think of another way of achieving the higher-level requirement (which you haven't described), rather than trying to force Hashtable to do something it was never supposed to do.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like it would be really, really helpful to know why the final result MUST be a Hashtable? That statement seems odd, and to me understanding that requirement is critical in this case.

So, let's ask again, why MUST the final design use a Hashtable??
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i can't change is because the class is to much used and its aprt of
Legacy application ( very big ) and i really don't know the impact thet
Changing the class will cause.
but if i will decide to change the class what will be the best
map type that i will extend from?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, anything that changes the order of elements will be "changing the class" to some extent. You need to consider things like:

Do you have access to all the code that uses the class you want to change?
Dou you have a good set of tests for the code, to verify that your changes didn't break anything?
How confident are you in your abilities to diagnose and fix subsequent problems in the code?
How bad is it if the application stops working?
How important is it that the order of the elements change?

It may be that the best choice here is to not change anything, if you can't predict how your changes will affect the existing application.

From what you've said, I think the method I described in my last post above would be the safest, if you (a) must change the order, but (b) can't test the results very well. This way the class is still an instanceof Hashtable (if anyone is testing that for any reason) and its methods are still synchronized, but the internal behavior is like a LinkedHashMap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic