Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

statement concerning comparable and comparator

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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.


 
Himai Minh
Ranch Hand
Posts: 1226
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 1226
6
  • Mark post as helpful
  • send pies
  • 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.
 
Luciano Leite
Greenhorn
Posts: 15
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Alexandre Leveille
Greenhorn
Posts: 12
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • 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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic