aspose file tools*
The moose likes Java in General and the fly likes Hashtable iterator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashtable iterator question" Watch "Hashtable iterator question" New topic
Author

Hashtable iterator question

Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Hi,

I have a Hashtable which I used iterate thru number of times in different methods.

//Let's say it is allocated already with some elements. When I say


It returns an iterator for the set of key elements, which are not sorted.

Assuming the key elements are 'A','B','C','D' , when I iterate thru it for the first time, I got iterator in an order of 'B', 'C', 'A', 'D'.

Now the next time I iterate thru the Hashtable again in another method, will I get the same order of Iterator 'B', 'C', 'A', 'D'. all the time?

Does anybody face this kind of scenario. I want the Iterator to return in the same order, whenever I use it. Any clues?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You're not guaranteed to get them in the same order, no.

Instead of using Hashtable, use TreeMap. The keys are sorted and always iterated in the same order.


[Jess in Action][AskingGoodQuestions]
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
But TreeMap is not that fast in retrieving the elements from right?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

TreeMap retrieval will have performance proportional to the log of the number of elements. So if you have a million elements in the map, then retrieving an element will (in theory) be 14 times slower than if there were just one element in the map. That's really not too bad. It's not constant time like the Hashtable, but if you want the keys kept in sorted order, then you have to pay for it.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Thank you very much. So, now I am using TreeMap in place of Hashtable. I want the TreeMap to be sorted based on 'values' not on 'keys'. So, I have my own 'Comparator' which sorts based on values. I was using the same to sort 'Hashtable', but when I use the same for TreeMap, I am getting the 'ClassCaseException'.



The Key for TreeMap is a 'Hashtable' object, and value is 'Double' object.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Finally I did it, by sorting the HashTable. For some reason, I am not able to create a TreeMap which is sorted based on values. I have explained why in my last reply. If any of you have clue about it, please leave a reply. Thank you...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Originally you just wanted to get a consistent order, not necessarily sorted. For that, I would recommend a LinkedHashMap, which will (by default) order the elements in the order they were inserted into the Map. Its performance for most operations is O(1), slower than HashMap but faster than TreeMap.

Now if you really want the entries sorted by value, using a custom Comparator in a single TreeMap fundamentally will not work. The Comparator is used to compare keys, and determine (among other things) if they're equal. If you try to use the comparator to compare values instead, the comparator will fail to detect equal keys, and the Map will become useless as you will be unable to retrieve a value using a key. The fact that you got ClassCastException probably indicates your Comparator was confused about whether it had a key or value. But fundamentally, there's no way to fix this within a single TreeMap.

I'm not sure I want to know what it means to "sort a Hashtable". But if it's working for you, good.

If each value is unique, I would probably use a second TreeMap in which the key and value are swapped. Or check out TreeBidiMap and DualTreeBidiMap from Jakarta Commons. If the values are not unique, then I'd do something like this:

Is that what you meant by sorting the Hashtable?


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

Joined: Mar 02, 2006
Posts: 53
@Jim Yingst,


I want a Map which gives me the iterator constanly, not necessarily sorted. As you said, LinkedHashMap may work.

Sorting a Hashtable:

I have used a custom 'comparator' which sorts the Hashtable based on values. Now, I get the Iterator in the sorted order. May be I can go with 'hashtable'. Thank you very much for your reply...

This is how I sort the Hashtable


Comparator Class:

[ May 16, 2007: Message edited by: ravi kotha ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Cool. Your sort was basically the same thing I did, except I see I omitted the part about casting to Map.Entry and using getValue(). Oops. The only reason your terminology confused me is that the Hashtable itself is still in the same unsorted order it was originally. But the Object[] array (or List in my example) has the contents of the Hashtable, sorted. Anyway, you probably know that - I'm just explaining my own confusion. Cheers...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashtable iterator question