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.
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.
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...
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:
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...
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...
Post by:autobot
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