• 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

statement concerning comparable and comparator

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is this statement FALSE?

You can use Java.lang.Comparable and Java.util.Comparator to sort collections whose elements are of any valid Java type, as long as all of the collection's elements are of the same class.


 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparator <T> and Comparable<T> are generic type.
In Comparable<T>, the compareTo(T t) takes a generic type T. So, it is supposed to compare objects of the same T type.
In Comparator<T>, the compare(T o1, T o2) takes a generic type T. So it is supposed to compare two objects of the same type T.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:Comparator <T> and Comparable<T> are generic type.
In Comparable<T>, the compareTo(T t) takes a generic type T. So, it is supposed to compare objects of the same T type.
In Comparator<T>, the compare(T o1, T o2) takes a generic type T. So it is supposed to compare two objects of the same type T.




Doesn't compareTo() take an Object? It seems that is what the Sierra and Bathes book says....
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, compareTo takes an object of any type. When you use compareTo to compare two objects, make sure these two objects are of the same type.
 
Greenhorn
Posts: 15
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Maybe you can compare two different classes that one extends another
or both of them implements a commom interface.
 
Greenhorn
Posts: 12
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has nothing to do with generics.

And although it is possible to compare two different classes that one extends another, it is possible to do something even more twisted than that.

Kathy Sierra & Bert Bates - Chp 7 Generics and Collections p.576 wrote:We've talked a lot about sorting by natural order and using Comparators to sort.The last rule you'll need to burn in is that, whenever you want to sort an array
or a collection, the elements inside must all be mutually comparable. In other words, if you have an Object[] and you put Cat and Dog objects into it, you won't be able to sort it. In general, objects of different types should be considered NOT mutually comparable, unless specifically stated otherwise.



That means it's a bad idea to compare Cat and Dog, but you could do it.

Here's an example of two totally different classes, mutually comparable:



Output is:
[Alexandre, Audi A3, Ford F-150, Glenn, Honda Fit, Michael, Sebastian, Wolkswagen Rabbit]

There you have it
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexandre Leveille wrote:This has nothing to do with generics.

And although it is possible to compare two different classes that one extends another, it is possible to do something even more twisted than that.

Kathy Sierra & Bert Bates - Chp 7 Generics and Collections p.576 wrote:We've talked a lot about sorting by natural order and using Comparators to sort.The last rule you'll need to burn in is that, whenever you want to sort an array
or a collection, the elements inside must all be mutually comparable. In other words, if you have an Object[] and you put Cat and Dog objects into it, you won't be able to sort it. In general, objects of different types should be considered NOT mutually comparable, unless specifically stated otherwise.



That means it's a bad idea to compare Cat and Dog, but you could do it.

Here's an example of two totally different classes, mutually comparable:



Output is:
[Alexandre, Audi A3, Ford F-150, Glenn, Honda Fit, Michael, Sebastian, Wolkswagen Rabbit]

There you have it



thank you! what a perfect example! now since you did a wonderful job of explaining this, can you please respond to my post about -classpath? thanks much!!!
 
Luciano Leite
Greenhorn
Posts: 15
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the question is false because you can compare any class you want (everybody extends Object).
In real life is not a good idea....and guess what? sometimes it IS necessary, because the model(legacy made by other people) needs so. And the happy path lays on implementing(overrinding Object version) Equals and HashCode methods properly...may the force be with you!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic