Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting objects using treeMap

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeMap sorts objects depends on keys. for that the object should implements Comparator/Comparable
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic