I need to pass a TreeMap as argument to a method. The TreeMap sorts the contents to its natural order. Is there any workaround for not having the TreeMap sort the contents. (leave the contents as it was originally added to it)
I do not have a choice of using a Map that doesnt sort - I need a TreeMap cos thats what a method thats outside my control expects.
Is this possible? Is it possible to implement a Comparator that does not sort? :-)
Thanks in advance!
Dhanya
dhanya kairali
Greenhorn
Joined: Dec 28, 2000
Posts: 22
posted
0
oops i will move this to the advanced forum. Thanks guys!
Is there any workaround for not having the TreeMap sort the contents. (leave the contents as it was originally added to it)
I do not have a choice of using a Map that doesnt sort - I need a TreeMap cos thats what a method thats outside my control expects.
Dhanya
That looks like bad design - If the method accepted any Map, you could pass it a LinkedHashMap which maintains the insert order.
If you really must use a TreeMap, you could subclass TreeMap and make it do what you want it to do. But that would be even worse, because you are breaking the contract declared by TreeMap, which is to maintain keys in the sorted order.
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