About Question enthuware.ocpjp.v8.2.1874 :
Moderator: admin
-
- Posts: 2
- Joined: Fri May 31, 2013 1:16 am
- Contact:
About Question enthuware.ocpjp.v8.2.1874 :
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!
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!
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
Not sure how exactly are you trying but java.util.Collections.sort(al, SortTest::diff); works fine.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri May 31, 2013 1:16 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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)
}
}
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)
}
}
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
Your code is missing the required diff method. It is there is the code given in the question.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sat Jun 03, 2017 1:43 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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 

-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Feb 06, 2018 11:14 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
Ok, but how does it know which diff method to invoke?
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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.mikehene wrote:Ok, but how does it know which diff method to invoke?
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Mar 02, 2020 3:38 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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.admin wrote: ↑Tue Feb 06, 2018 10:41 pmThe 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.mikehene wrote:Ok, but how does it know which diff method to invoke?
-
- Posts: 9
- Joined: Thu Feb 02, 2023 12:18 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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);}
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);}
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
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:
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:
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.
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);
}
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Thu Feb 02, 2023 12:18 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1874 :
Finally I understand how this works
thank you!

Who is online
Users browsing this forum: Bing [Bot] and 2 guests