Win a copy of Functional Reactive Programming this week in the Other Languages forum!
    Bookmark Topic Watch Topic
  • New Topic

A tree map that doesnt sort

 
dhanya kairali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator
oops i will move this to the advanced forum. Thanks guys!
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Continue discussion at http://www.coderanch.com/t/327503/java/java/tree-map-doesnt-sort
 
    Bookmark Topic Watch Topic
  • New Topic