Comparator & Comparable
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
I find that Interface Comparator and Comparable both
have the method to compare. Who can tell me what the
difference between them?
Thank you.
I find that Interface Comparator and Comparable both
have the method to compare. Who can tell me what the
difference between them?
Thank you.
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Comparable is an interface that a class can implement.
However, in order to use Comparable, you have to have access to the source code of the class.
The Comparator interface allows us to define a comparison between two objects when we may not have access to the source code.
Another use of the Comparator interface is that you can use it in conjuction with a Collection to sort the Collection in any way you want without having to change the class definition of the objects in the Collection.
However, in order to use Comparable, you have to have access to the source code of the class.
The Comparator interface allows us to define a comparison between two objects when we may not have access to the source code.
Another use of the Comparator interface is that you can use it in conjuction with a Collection to sort the Collection in any way you want without having to change the class definition of the objects in the Collection.
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Comparable - Something that can be compared with another.
Comparator - Something that can compare two things.
If you understand the concept (it really is just the meaning of the two english words), the rest will be easy.
1. Comparable has compareTo() method that takes only one argument (the object with which 'this' object has to be compared) and Comparator has compare() method that takes two arguments (the objects that need to be compared.)
2. When a class knows how its objects can be compared, it implements Comparable. When *you* want to compare two objects *your* way (instead of the class's implementation), you create a new class that implements a Comparator and pass the two objects to its compare method.
HTH,
Paul.
Comparator - Something that can compare two things.
If you understand the concept (it really is just the meaning of the two english words), the rest will be easy.
1. Comparable has compareTo() method that takes only one argument (the object with which 'this' object has to be compared) and Comparator has compare() method that takes two arguments (the objects that need to be compared.)
2. When a class knows how its objects can be compared, it implements Comparable. When *you* want to compare two objects *your* way (instead of the class's implementation), you create a new class that implements a Comparator and pass the two objects to its compare method.
HTH,
Paul.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| "How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






