So... I was told that using Comparable and Comparator in the same class is a bad .
I've done it,because ... Eclipse suggest me this silly idea. It obviously means that i didn't understand difference between them,so it was time for do little research and here are my notes:
Definitions:
Comparable:
A comparable is a interface and object that implement this interface is capable of comparing itself with another object. To implement this interfaced class must contain a method called compareTo.
Comparator:
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
Look for below links for more complete information about Comparable and Comparator: (Where they will show example how to implementing sorting for List and other collections ):
java sorting - comparator vs comparable
javablogging - comparable vs comparator
DOMINIK SYMONOWICZ's blogs - FOREVER HUNGRY of knowledge, sarcasm, smiling, music, dancing, and food ...lots of food. This IT blog is a collection of notes with a simple solution with an explanation, added documentation, sarcastic comment and weird example. I also write about a lesson learnt from my mistakes. A non-it blog can be found here: Dominik Symonowicz's non-IT blog and website
Showing posts with label comparable. Show all posts
Showing posts with label comparable. Show all posts
Subscribe to:
Posts (Atom)