Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A tree map that doesnt sort

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
oops i will move this to the advanced forum. Thanks guys!
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Continue discussion at https://coderanch.com/t/327503/java/java/tree-map-doesnt-sort
 
    Bookmark Topic Watch Topic
  • New Topic