1Z0-815 "The sort method" confusion

Post Reply
dimitrilc
Posts: 29
Joined: Sat Jun 06, 2020 4:51 pm
Contact:

1Z0-815 "The sort method" confusion

Post by dimitrilc »

Hi, on page 365 in this book, the paragraph mentioned the functional method

Code: Select all

int compare(T o1, T o2)
, which belongs to the Comparator<T> interface.

But the example code you gave uses the functional method

Code: Select all

int compareTo​(T o)
, which does NOT belong to the Comparator<T> interface, but it actually belongs to the Comparable<T> interface.

Is this an error or is there some gap in my knowledge?

admin
Site Admin
Posts: 8907
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: 1Z0-815 "The sort method" confusion

Post by admin »

Are you talking about Section 14.1.5 under heading "The sort Method"? Because I see it on page 361 (not 365).

The paragraph is correctly talking about java.util.Comparator's compare method:
The sort method
A collection has no notion of order but a list does. It makes sense, therefore, that List interface has a default sort(Comparator<? super E>comparator) method which allows you to sort the elements of this list using the sorting order determined by the comparator. The java.util.Comparator interface has been around since Java 1.2 but it has been made a functional interface in Java 8. Its functional method is int compare(T o1, T o2), which compares its
two arguments and returns a negative integer, zero, or a positive integer if the first argument is less than, equal to, or greater than the second.
The code that it shows below this paragraph i.e. games.sort( (a, b)->a.compareTo(b)); is also correct.

(a, b)->a.compareTo(b) is a lambda expression used to build a Comparator object. This expression uses String's compareTo method. The fact that String implements Comparable is not relevant here. This expression will be translated to a compare method like this:

Code: Select all

class SomeMadeUpNameComparator implements Comparator{ 
   public int compare(String a , String b){
      return a.compareTo(b); //<---- content from the lambda expression
   }
}
May be this point should me made more clear. I will make a note for the author.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dimitrilc
Posts: 29
Joined: Sat Jun 06, 2020 4:51 pm
Contact:

Re: 1Z0-815 "The sort method" confusion

Post by dimitrilc »

Hi Paul, Thanks for the response.

It is indeed on page 361, but the Kindle app was showing the wrong page number for some reason. I rebooted the app and it is fine now.

The point of confusion was the author saying that the compare method returns some integer. But in fact, compare was just an empty abstract method, and one needs to override it to use it.

There was no explanation of the compareTo method, and it was used in the example, so when I looked it up, I found that there are two different interfaces with similar names, Comparator and Comparable. I did not know that String implements Comparable until I read your post.

I was able to find a second opinion on this confusion, and they exposed the lambda to me exactly like you did. Initially I was told that compare is empty and has no implementation, but it is different to what the book says, so I went into the java api source code to verify this and I understand this topic a little bit more now.

Thank you again for your explanation.

admin
Site Admin
Posts: 8907
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: 1Z0-815 "The sort method" confusion

Post by admin »

>>>The point of confusion was the author saying that the compare method returns some integer.

This statement is correct. The method's return type is int and so, it is correct to say that this method returns an int.

>>>But in fact, compare was just an empty abstract method, and one needs to override it to use it.

The method signature defines what a method returns. That is what it is talking about. Yes, it needs to be implemented and that is what is being done in the lambda expression. That's the whole point of a functional interface!

The fact that it is abstract is also actually obvious because the statement starts with, "Its functional method is int compare(T o1, T o2), ....". It makes it clear that it is a functional method. The concept of functional method is already explained in detail in 14.1.4. It tells you that it is abstract. So, you should know at this stage that it is abstract.

If you go through the chapter in order, then it will be clear. If you jump in the middle, then, obviously, you will not get the complete chain of thought.

There is no explanation to compareTo because it is not important at that point. Previous sections already explain how a method of a class is used in a lambda expression, which in turn forms the method body of the functional method of the functional interface. The fact that compareTo belongs to Comparable interface is absolutely irrelevant here. It is just some method that exists in String class and we are using that method to implement the Comparator interface through a lambda expression.

Still, your point is noted and it will be enhanced in the book.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest