-1

I would like to know why java Java provides two different interfaces(Comparable/Comparator) to compare objects. I thought instead of proving a new interface(Comparator) to compare objects with multiple attribute, Java can simply extended "Comparable" interface to achieve the "Comparator" interface logic.

Can't we achieve the same only by using "Comparable"? . Say some 3rd party written an comparison logic by implementing "Comparable" first. Why can't we implement a new class with "Comparable" and pass the comparable instance to the sorting API?

Both are introduced in the same Java version 1.2 . I am not able to understand why two different interface to achieve same functionality.

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Comparable.java#Comparable

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Comparator.java

JavaUser
  • 25,542
  • 46
  • 113
  • 139
  • @Bathsheba , my question is about , why Java is designed like this?. This is not about when to use comparator and comparable . And also this is not related to the difference between comparator and comparable . Please understand my question. – JavaUser Jul 14 '17 at 10:24
  • 1
    my question is about , why Java is designed like this? <- So you rather want this question closed as primarly opinion based? – OH GOD SPIDERS Jul 14 '17 at 10:27
  • This is not opinion based I guess . There should be some strong reason for this which someone might knowing as we dont. – JavaUser Jul 14 '17 at 10:31
  • Because both the interfaces are introduced in same Java version 1.2 . I am not able to understand why two different interface required to achieve a single functionality. – JavaUser Jul 14 '17 at 10:35
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Comparator.java – JavaUser Jul 14 '17 at 10:35
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Comparable.java#Comparable – JavaUser Jul 14 '17 at 10:35

1 Answers1

1

Those are two different functionalities.

Comparable denotes: "I can compare myself against another object of my kind".

Comparator says: "I know how to compare two objects of a certain kind".

Those are different use cases - and you simply want both - just for different situations.

Example: when you want to sort some objects that are coming from an external library. The corresponding class does not implement Comparable - but you can still sort them by providing a Comparator.

Another example: assume you have a list of objects that have various attributes. Now you can create different Comparators for that one class; and each one compares a different field - allowing you to sort a List<Person> on Person.getName(), or Person.getAge(), ...

On the other hand, you have classes like Integer - where it simply makes sense that one Integer knows how to compare itself against another Integer. Simply because the natural order of numbers should be a part of your object model as well.

Beyond that: it seems that you don't get my point. Thing is: those two concepts are simple two different abstractions. Of course you can build something complicated that expresses what the one concept does in terms of the other concept. But that is like using a hammer to get a screw into the wall. It simply creates more problems than it will be solving. Your suggestion to have another class implement Comparable in order to sort in a different way - boils down to instantiate one addition object per list entry. Not efficient at all. And also very much counter intuitive for any experienced Java programmer. You don't invent your own wheel unless you have to. And you forget about inventing your own wheel when it is clear right from scratch that this wheel will be deficient compared to the already existing, well known and widely used wheel.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Can't we achieve the same only by using "Comparable"? . Say some 3rd party written an comparison logic by implementing "Comparable". Why can't we implement a new class with Comparable and pass the comparable instance to the sorting API? – JavaUser Jul 15 '17 at 08:48
  • 1
    So. I reopen the question. I give a lengthy answer. What comes back? More questions. Okay, anyway: take your example and really write down that code. And then look how complicated it will be to implement a reasonable "comparator like" construct - using another comparable class x to compare objects of class y. Of course that is possible - but it would be extremely inefficient and simple feel like a dirty hack. You would end up with a design that is broken right from the beginning. – GhostCat Jul 15 '17 at 12:13
  • Sure . Let me try that. – JavaUser Jul 17 '17 at 04:43