• 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
  • 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

Sorting objects using treeMap

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeMap sorts objects depends on keys. for that the object should implements Comparator/Comparable
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic