aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes statement concerning comparable and comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "statement concerning comparable and comparator" Watch "statement concerning comparable and comparator" New topic
Author

statement concerning comparable and comparator

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
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

Joined: Jul 29, 2012
Posts: 758
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

Joined: Oct 24, 2012
Posts: 95
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

Joined: Jul 29, 2012
Posts: 758
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

Joined: Mar 01, 2006
Posts: 15

Hi,
Maybe you can compare two different classes that one extends another
or both of them implements a commom interface.
Alexandre Leveille
Greenhorn

Joined: Jan 26, 2013
Posts: 12

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

Joined: Oct 24, 2012
Posts: 95
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

Joined: Mar 01, 2006
Posts: 15

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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: statement concerning comparable and comparator