aspose file tools*
The moose likes Beginning Java and the fly likes A tree map that doesnt sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

A tree map that doesnt sort

dhanya kairali
Greenhorn

Joined: Dec 28, 2000
Posts: 22
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
oops i will move this to the advanced forum. Thanks guys!
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Continue discussion at http://www.coderanch.com/t/327503/java/java/tree-map-doesnt-sort


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A tree map that doesnt sort