Forums Register Login

Hashtable iterator question

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
But TreeMap is not that fast in retrieving the elements from right?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

@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 ]
+Pie Number of slices to send: Send
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'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2159 times.
Similar Threads
Chishlom's question
order of elements returned by Iterator in Set
Ordered and Sorted
Which collection to use?
Sorting HashTable by values
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 21:33:12.