Page 1 of 1

About Question enthuware.ocpjp.v8.2.1874 :

Posted: Thu Dec 10, 2015 9:26 am
by xvpower
I try the code!

 java.util.Collections.sort(al, (p1, p2)->p1.dob.compareTo(p2.dob)); ok
java.util.Collections.sort(al, new MySorter()::compare); ok
java.util.Collections.sort(al, SortTest::diff); not run
java.util.Arrays.sort(al, SortTest::diff); not run

why? Anser 3
thk!

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Thu Dec 10, 2015 10:48 am
by admin
Not sure how exactly are you trying but java.util.Collections.sort(al, SortTest::diff); works fine.
-Paul.

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Thu Dec 10, 2015 8:06 pm
by xvpower
thank!
I Copy Paste Question code !

class Person{
String name;
String dob;
public Person(String name, String dob){
this.name = name; this.dob = dob;
}
}
class MySorter {
public int compare(Person p1, Person p2){
return p1.dob.compareTo(p2.dob);
}
}

public class SortTest {
public static int diff(Date d1, Date d2){
return d1.compareTo(d2);
}
public static void main(String[] args) {
ArrayList<Person> al = new ArrayList<>();
al.add(new Person("Paul", "01012000"));
al.add(new Person("Peter", "01011990"));
al.add(new Person("Patrick","01012002"));

java.util.Collections.sort(al,SortTest::diff); <---no suitable method found for sort(ArrayList<Person>,SortTest::diff)

}

}

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Thu Dec 10, 2015 10:02 pm
by admin
Your code is missing the required diff method. It is there is the code given in the question.

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Sun Jan 21, 2018 2:14 am
by JavaSoftware
Hi, sort(List<T> list, Comparator<? super T> c) expects Comparator as a second parameter but classes SortTest and MySorter doesnt implement this interface, so i am little confused :D

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Sun Jan 21, 2018 3:43 am
by admin
In Java 8, Comparator is also a functional interface. So you can use a lambda expression to implement a Comparator, which is what the code in this question is doing.

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Tue Feb 06, 2018 11:16 am
by mikehene
Ok, but how does it know which diff method to invoke?

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Tue Feb 06, 2018 10:41 pm
by admin
mikehene wrote:Ok, but how does it know which diff method to invoke?
The compiler figures it out by looking at the generic type of al that is passed to Collections.sort as the first argument. Since this al refers to an ArrayList that is typed to Person, it knows that the context of the lambda expression SortDiff::sort has Person object available, and therefore, it uses diff(Person p1, Person p2) version to convert the lambda expression into real method call.

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Fri Apr 16, 2021 4:34 pm
by Sieusc
admin wrote:
Tue Feb 06, 2018 10:41 pm
mikehene wrote:Ok, but how does it know which diff method to invoke?
The compiler figures it out by looking at the generic type of al that is passed to Collections.sort as the first argument. Since this al refers to an ArrayList that is typed to Person, it knows that the context of the lambda expression SortDiff::sort has Person object available, and therefore, it uses diff(Person p1, Person p2) version to convert the lambda expression into real method call.
Came in to ask this very question. Interesting, do you know of any specific resources that provide more insight as to these kind of rules with regards to method references/inferences? I have read a most famous OCP 8 book, like many, but I feel it lacks important information on a few aspects of the material which is needed to successfully answer all the questions.

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Thu Feb 02, 2023 12:29 pm
by robhun79
Hi all,
There is one thing i dont understand.
The Comparator interface is a functional interface and it's abstract method is compare.
So how can we return compareTo in the SortTest methods? Why does this work and why does it not need to have compare?

public class SortTest {
public static int diff(Person p1, Person p2)
{return p1.dob.compareTo(p2.dob);}

public static int diff(Date d1, Date d2)
{return d1.compareTo(d2);}

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Fri Feb 03, 2023 1:13 am
by admin
SortTest doesn't claim to implement Comparator. It is just a class that has a method named diff and that method returns p1.dob.compareTo(p2.dob); We are making use of this diff method from another object that implements Comparator.

For example, when we do new java.util.Arrays.sort(al, SortTest::diff); we are telling the compiler to do the following things:
1. create an class that implements Comparator interface. So, it creates something like this:

Code: Select all

//This is not exact. I am just giving a rough idea here.
_Comparator implements Comparator{
   public int compare(Person obj1 , Person obj2){

      //some code here
   }
}

2. then we are asking it to call the static diff method of SortTest inside the compare method of the above code and return the value returned by that method.
So:

Code: Select all

_Comparator implements Comparator{
   public int compare(Person obj1 , Person obj2){
       return SortTest.diff(obj1, obj2);
   }
}
So, you can see that a compare method as required by the Comparator interface is indeed present. It is just that the compiler created all this code on its own just by looking at SortTest::diff.

Re: About Question enthuware.ocpjp.v8.2.1874 :

Posted: Sat Feb 04, 2023 7:48 am
by robhun79
Finally I understand how this works :) thank you!