This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sorting objects using treeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting objects using treeMap" Watch "Sorting objects using treeMap" New topic
Author

Sorting objects using treeMap

raj talatam
Ranch Hand

Joined: Apr 19, 2012
Posts: 87
TreeMap sorts objects in Natural Order .
if integers are in 1,2,3.. etec
String are in alphabetical order ..

what about Objects.

if car objects are there how they will be sorted
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

TreeMap sorts objects depends on keys. for that the object should implements Comparator/Comparable
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

I think you mis-phrased that. It sounds like making the keys implement Comparator will work, but that's not true.

There are two possibilities:
1) The keys implement Comparable and are mutually comparable (i.e. an Integer and a String in one TreeMap would cause errors).
2) A separate Comparator is used; it has no connections with the keys other than the ability to compare them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
raj talatam
Ranch Hand

Joined: Apr 19, 2012
Posts: 87
Seetharaman Venkatasamy wrote:TreeMap sorts objects depends on keys. for that the object should implements Comparator/Comparable

can you elaborate,
for example

what is natural in this case
i cant run this code i am getting class cast exception
info.inetsolv.Car cannot be cast to java.lang.Comparable
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

raj talatam wrote:
Seetharaman Venkatasamy wrote:TreeMap sorts objects depends on keys. for that the object should implements Comparator/Comparable

can you elaborate,
for example

what is natural in this case
i cant run this code i am getting class cast exception
info.inetsolv.Car cannot be cast to java.lang.Comparable



Basically, the "natural" order is the order defined by the Comparable interface. If the element doesn't support the Comparable interface, then it doesn't have a natural order, and if you try to use a TreeMap (without a Comparator), then you will get the cast exception.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

Objects don't automatically have a natural order. A natural ordering is the one that make most sense given what the objects represent, but the computer doesn't know anything about that, so you have to tell it. And you tell it by implementing Comparable. The reason it already works for Integer and String is that the people who wrote those classes have already done this. But for your own classes, you have to.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Also, take a look at This Java Tutorial Page. It describes how to use the Comparable interface and when / how to use the Comparator interface.


Steve
 
GeeCON Prague 2014
 
subject: Sorting objects using treeMap