Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes How to keep the order of elements in hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to keep the order of elements in hashtable" Watch "How to keep the order of elements in hashtable" New topic
Author

How to keep the order of elements in hashtable

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Use the java.util.LinkedHashMap instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

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

Joined: Apr 27, 2006
Posts: 597
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

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Oct 30, 2001
Posts: 1970
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

Joined: Apr 27, 2006
Posts: 597
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Bill Shirley:

SortedMap & TreeMap are what you're interested in.


No, that's not what he wants, as Peter already explained.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Oct 30, 2001
Posts: 1970
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

Joined: Oct 14, 2002
Posts: 8803
    
    5
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??


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Jan 30, 2000
Posts: 18671
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to keep the order of elements in hashtable
 
Similar Threads
Hashtable / HashMap Problem.
ordering Hashtable elements
Hashtable
how to prove that Hashtable is not ordered
reading elements in hashtable in an order