About Question enthuware.ocpjp.v8.2.1874 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
xvpower
Posts: 2
Joined: Fri May 31, 2013 1:16 am
Contact:

About Question enthuware.ocpjp.v8.2.1874 :

Post 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!

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

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

Post by admin »

Not sure how exactly are you trying but java.util.Collections.sort(al, SortTest::diff); works fine.
-Paul.
If you like our products and services, please help us by posting your review here.

xvpower
Posts: 2
Joined: Fri May 31, 2013 1:16 am
Contact:

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

Post 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)

}

}

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

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

Post by admin »

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.

JavaSoftware
Posts: 6
Joined: Sat Jun 03, 2017 1:43 pm
Contact:

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

Post 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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

mikehene
Posts: 1
Joined: Tue Feb 06, 2018 11:14 am
Contact:

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

Post by mikehene »

Ok, but how does it know which diff method to invoke?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

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

Post 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.

robhun79
Posts: 9
Joined: Thu Feb 02, 2023 12:18 pm
Contact:

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

Post 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);}

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

robhun79
Posts: 9
Joined: Thu Feb 02, 2023 12:18 pm
Contact:

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

Post by robhun79 »

Finally I understand how this works :) thank you!

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 45 guests