• Post Reply 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

A tree map that doesnt sort

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answered here -

https://coderanch.com/t/401025/java/java/tree-map-doesnt-sort

Dhanya, If you are going to post a topic again in a different forum, It is better to delete the original topic. Cheers.
 
dhanya kairali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sonny Gill:
Answered here -

https://coderanch.com/t/401025/java/java/tree-map-doesnt-sort

Dhanya, If you are going to post a topic again in a different forum, It is better to delete the original topic. Cheers.




I have deleted the post from the beginner forum.
Here is the response posted there >

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.
--------------------------------------------------------------------------------
Posts: 825 | Registered: Feb 2002 | IP: Logged
 
dhanya kairali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could not delete post

"Only administrators or moderators may perform this action. "
:-(

I know its bad design but I am stuck so what I wanted was a workaround.
Tried writing a Comparator that doesnt compare so that I could call a
new TreeMap(MyComparator()) but my comparator logic (where I return 1 from within the compare method always) isnt working

I suppose I darent hope that someone out there must have implemented a Comparator that does not compare!!! :-)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you write a comparator that doesn't compare, the TreeMap will stop working - it won't any longer find your objects in the Map.
 
dhanya kairali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats what i found when i used the comparator i implemented, i thot my comparator logic was wrong. :-(
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason it stops working is that the TreeMap doesn't only sort the keys, it only uses a search algorithm that relies on a valid sorting to improve search performance.

Can you tell us what the method that's expecting the TreeMap is doing with it? Why can't you change it?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about trying this workaround? I'm not sure if it's quite right for you, but it's a place to start. By the way, I'm sorry that you have to deal with such an inflexible API.

Create a class that wraps the objects you're inserting into your TreeMap.

Give the new class (InsertOrderObject ?) an int instance variable like this:
private int insertIndex;

Keep a counter someplace (maybe a static for InsertOrderObject?) and increment it every time you insert one of these things to your TreeMap.

Make it so that each object you insert into your TreeMap has an incremented insertIndex. E.g., the first one would have insertIndex == 0, the second insertIndex == 1, etc.

Now, you can create a Comparator for your new InsertOrderObject class that is based on the insertIndex.

...
Ok, now back up a bit (sorry). First, insert all of the objects you would have inserted into your TreeMap into a Map that maintains insert order.

Now, iterate through your Map. Create a new incremented InsertOrderObject based on the object retrieved from the Map. Add the InsertOrderObject to your TreeMap.

Once you've completely iterated through your original Map, your TreeMap will consist of InsertOrderObjects sorted by the original order in which you inserted the underlying objects in your original Map.

This might work . . .
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic