File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Using Comparator with TreeNap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Comparator with TreeNap " Watch "Using Comparator with TreeNap " New topic
Author

Using Comparator with TreeNap

pradeep selvaraj
Ranch Hand

Joined: Nov 29, 2005
Posts: 62
I have a TreeMap as follows

TreeMap<String,Integer> mymap = new TreeMap<String,Integer>

I want mymap to be sorted according to the integer field, using Comparator. Can anyone tell me how it can be done.

Thanks

Serish


[<a href="http://cyvis.sourceforge.net" target="_blank" rel="nofollow">CyVis - Software Complexity Visualiser</a>] [<a href="http://pradeepselvaraj.blogspot.com" target="_blank" rel="nofollow">Pradeep's Blog</a>]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Look in the API specification for TreeMap, and see which way it is sorted.
In most maps, you have a key and a value, and you use the key for inserting and finding the values.
I presume by integer, you mean Integer.
Go to the API, which we should be reading all the time.
Find out whether Integer already has a compareTo<Integer>() method, and whether it implements the Comparable<Integer> interface.
Find out whether your TreeMap has to be sorted or is sorted automatically.

I think once you have found all that lot out, your answer to your query will be very easy. Then try it out and tell us what happens.

CR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Do you need a Comparator at all?
CR
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I think you're missing the point -- he wants to sort on the value, not the key.

Leaving aside the question of whether this is a good idea or not, and just looking at it as a programming challenge: The Comparator is going to need a reference to the Map, so it can find the values for the keys it will be passed as arguments. Since the Comparator is a constructor argument to the Map, this will be a multi-step process. But basically, you could do something like this (untested



Let us know if you get this crazy scheme to work!


[Jess in Action][AskingGoodQuestions]
pradeep selvaraj
Ranch Hand

Joined: Nov 29, 2005
Posts: 62
Yeah Ernest you are correct. I wanted to sort the treemap using the value instead of the key. I tried out using a treeset along with the treemap, Like this



Now, iterating through the set gives me the results sorted according to the Integer (ie the value field in the tree map). But when i try using the same comparator with the TreeMap directly(as show below), i get a type cast exception.




Yeah this sorthing could have been done simply by using a class that implements comparable, but my assignment required me to use data structures, that's why i had to do it this way.

Thanks

Serish
[ May 02, 2006: Message edited by: serish selvaraj ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Comparator with TreeNap
 
Similar Threads
ClassCastException
Can I have a list in tree map?
General questions about Map interface.
sorting hashmap based on values
Why will TreeMap<K,V>(Comparator) not compile?