Forums Register Login

A tree map that doesnt sort

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
oops i will move this to the advanced forum. Thanks guys!
+Pie Number of slices to send: Send
 

Originally posted by dhanya kairali:

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.
+Pie Number of slices to send: Send
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


Reply locked
This thread has been viewed 907 times.
Similar Threads
Distinct list ov values from a Map
TreeMap containsKey issue
Sorted keys from HashMap
TreeMap Sorting Problem!
Collections problem
More...

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