This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparable Interface In java  RSS feed

 
Brayn Richard
Greenhorn
Posts: 14
Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I was asked in last interview about comparable Interface.
They asked me about difference between comparable and comparator. I was able to give them an idea about comparator but could not answer it properly as I dunno about comparable.
Any references?
Google shows me articles from 2010-2013 which is too old I guess.
(Of course I didn't get the job )



 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brayn Richard wrote:So, I was asked in last interview about comparable Interface.
They asked me about difference between comparable and comparator. I was able to give them an idea about comparator but could not answer it properly as I dunno about comparable.
Any references?
Google shows me articles from 2010-2013 which is too old I guess.
(Of course I didn't get the job )

The Oracle documentation provides a good impression.

I prefer comparable because it's more "light weight". You only have to implement the compareTo method and don't have an overhead of an additional class. comparable by design specifies the "natural" order which however is up to you to specify. I would rather use a comparator if I had more than one order to sort data. If you had five sort orders for a collection of objects you could just write five comparators that are completely independant from each other. You could also use more complex algorithms without polluting the original class with code that does not necessarily belong there. As instance and comparator are decoupled you are also more flexible and can easily swap comparators on the fly whereas you would have to resort to inheritance for swapping the algorithm of your compareTo method or you would use some flags within your instance to determine what algorithm is used which will likely lead to a not very elegant implementation. These are just some spontaneous thoughts, I'm curious what others have to say about comparable vs comparators.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:. . . I prefer comparable because it's more "light weight". . . .
Disagree there. It has nothing to do with being lightweight or heavyweight (actually those are the wrong terms, which should be applied to GUI components). You mentioned total ordering but I am not sure you understand that concept fully. It means you take two instances of the same class and can say, “x is more than y,” or, “x is less than y,” or, “x is the same as y,” for all x and y. If you look at the link you provided yourself, you will find that 111 of the approx 4200 classes in Java7 implemented Comparable, which means that 4100 classes do not have a total ordering. The reason for using a Comparator rather than Comparable is that Comparator provides much more flexibility. You said yourself “more than one oreder”; if you have only one possible order, then you will have a total ordering. Last sentence before this point added as an “edit”.
How are you going to order these four objects:-
  • potatoes, 10kg bag
  • tomatoes, 1kg packet
  • potatoes, 5kg bag
  • tomatoes, 500g packet?
  • It should be obvious how the Strings "Campbell Ritchie" and "Christian Pflugradt" are ordered, but what about the two Name objects:-
  • Campbell, Ritchie
  • Christian, Pflugradt
  • That is why you have Comparators. Yes, until Java7 it took much longer to write a Comparator but in Java8 you can simply writeWhat will happen if you swap lines 3 and 4? You can now have options to sort the vegetables by name or by size. You can now sort the people by first name or by last name (as in a telephone book).
    In Java8 you can even add a method to such a declaration which does both sorts in one pass through the List.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7808
    142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just want to add two points to what Campbell said:

    A type can have a natural total ordering, and you may still want to sort it in a different way. Comparable is used for the natural order, and Comparator is used for everything else.

    As mentioned earlier, Java 8 makes it a lot easier to create custom Comparators. You can use method handles and comparator chaining to create very fluent code:

    This requires that you have static import on Comparator's members though:
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Brayn Richard wrote:They asked me about difference between comparable and comparator....

    There is another subtle difference between Comparable and Comparator that hasn't been mentioned: Comparable is implemented by a type; whereas Comparators are implemented for a type - and while this may not seem earth-shattering, it means that compareTo() is an instance method of the type being compared, whereas Comparator.compare() isn't.

    This, in turn, means that compareTo() MUST throw a NullPointerException if the argument it's passed is null. This is because it must be both commutative and transitive (see the methods docs for explanations), and since, in order to call the method, the "calling" object cannot be null, it follows that the argument can't be null either.

    Comparators, on the other hand, don't have this restriction (unless they impose it themselves), since they aren't bound to a specific object.

    HIH

    Winston
     
    Paweł Baczyński
    Bartender
    Posts: 2054
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:It should be obvious how the Strings "Campbell Ritchie" and "Christian Pflugradt" are ordered.

    Well, it sorting Strings stops being obvious when you consider internationalization and collators.
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:. . . compareTo() MUST throw a NullPointerException if the argument it's passed is null. . . .
    I had forgotten that bit. Yes, the Comparable documentation does say “throws NullPointerException”.
     
    James Ryder
    Greenhorn
    Posts: 5
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Brayn Richard wrote:So, I was asked in last interview about comparable Interface.
    They asked me about difference between comparable and comparator. I was able to give them an idea about comparator but could not answer it properly as I dunno about comparable.
    Any references?
    Google shows me articles from 2010-2013 which is too old I guess.
    (Of course I didn't get the job )


    Well, I was looking for the same as you few weeks ago and I was basically following Oracle Documentation
    I also found This helpful.

    And I am thankful to Campbell Ritchie to make it more clear. Yeah your points makes sense, its nothing to do with weight
     
    Brayn Richard
    Greenhorn
    Posts: 14
    Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all for your answers, specially Campbell Ritchie.
    And also James Ryder for your resource
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!