[HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

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

Moderator: admin

Post Reply
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

[HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by flex567 »

What imports are used for the following code ?

Code: Select all

ArrayList < Integer > iList = new ArrayList < >(); 
iList.addAll( Arrays.asList( 1, 2, 3, 4, 5, 6)); 
Predicate < Integer > p = x-> x% 2 == 0; 
iList.removeIf( p); 
System.out.println( iList);

How would this code look like if the Lambda expression woudn't be used?

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

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by admin »

Replace this line: Predicate < Integer > p = x-> x% 2 == 0;
With:

Code: Select all

Predicate < Integer > p = new Predicate<Integer>(){
          public boolean test(Integer x){
            return x% 2 == 0; 
          }
      };
Equivalency of lambda and anonymous inner class is explained in 12.5.1 and the usage of Predicate without lambda is shown in 12.5.3.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by flex567 »

In the Car Mall example we are not instantiating a Predicate object.
Could we use the Predicate object in the Car Mall example like we are using it here?

There wasn't much in the book about Predicate class, only about the Predicate interface and its methods. Is that becuase it belongs more to the next level exam?

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

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by admin »

1. There is no class named Predicate. There is just an interface named Predicate. Just near the begining of section 12.5.3, the book says, "Filtering through a list of objects is such a common requirement in Java application that the Java standard library includes a generic interface for this purpose - java.util.function.Predicate.".

2. Yes, you can use a Predicate object in the CarMall example. Not sure if you have gone through Section 12.5.3 carefully but it explains exactly the question that you have asked here. It shows exactly how you can replace CarFilter of CarMall example with a Predicate.

3. OCAJP exam requires you to know only the Predicate interface and its usage with ArrayList. The book discusses all that you need to know about this for the exam in Section 12.5.3 and 12.5.4. So, go through these two sections very carefully.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by flex567 »

I thought there is a class named Preciate because of: Predicate <Integer> p = new Predicate<Integer>(){...

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

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by admin »

It is instantiating an anonymous class that implements Predicate interface. The concept of anonymous class is explained in section 9.2.7.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by flex567 »

This is how I implemented it:

Code: Select all


import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Lambda1 {

    public static void main( String[] args) {


		Predicate <Car> p = new Predicate<Car>(){
          public boolean test(Car c){
            return c.company.equals(" Honda");
          }
		};
		
		// Lambda Expression
       CarMall cm = new CarMall();
       List < Car > carsByCompany = cm.showCars(p);
	   
	   System.out.println( carsByCompany);
		
    }
}

class Car {

    String company;
    int year;
    double price;
    String type;

    Car( String c, int y, double p, String t){
        this.company = c;
        this.year = y;
        this.price = p;
        this.type = t;
    }

    public String toString(){ return "(" + company +" "+ year +")"; }
}

class CarMall {
    List<Car> cars = new ArrayList< >();

    CarMall() {
        cars.add(new Car(" Honda", 2012, 9000.0, "HATCH"));
        cars.add(new Car(" Honda", 2018, 17000.0, "SEDAN"));
        cars.add(new Car(" Toyota", 2014, 19000.0, "SUV"));
        cars.add(new Car(" Ford", 2014, 13000.0, "SPORTS"));
        cars.add(new Car(" Nissan", 2017, 8000.0, "SUV"));
    }

    List <Car> showCars(Predicate<Car> cf){
        ArrayList <Car> carsToShow = new ArrayList < >();

        for( Car c : cars){
            if( cf.test(c)) 
                carsToShow.add(c);
        }
        return carsToShow;
    }	
}

Can lambda be used without predicate interface?

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

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by admin »

I don't see any lambda expression being used in your code.
Yes, of course, lambda expression can be used without Predicate interface. Code on Page 366 shows how -

Code: Select all

public class TestClass{
  public static void main(String[] args) {
     CarMall cm = new CarMall();
     List<Car> carsByCompany = cm.showCars(c -> c.company.equals("Honda"));
     System.out.println(carsByCompany);
  }
}

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by flex567 »

I don't see any lambda expression being used in your code.
My code was created because of this post:

Code: Select all

2. Yes, you can use a Predicate object in the CarMall example. Not sure if you have gone through Section 12.5.3 carefully but it explains exactly the question that you have asked here. It shows exactly how you can replace CarFilter of CarMall example with a Predicate. 

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by OCAJO1 »

As you have probably guessed, the lambda expression returns true if an element is even or not.
I think the point will be a lot more clear if 'or not' is removed.

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

Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]

Post by admin »

Right.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests