Page 1 of 1
[HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Sun Nov 18, 2018 11:25 am
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?
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Sun Nov 18, 2018 8:41 pm
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.
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Sun Dec 02, 2018 11:42 am
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?
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Sun Dec 02, 2018 12:07 pm
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.
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Sun Dec 02, 2018 12:29 pm
by flex567
I thought there is a class named Preciate because of: Predicate <Integer> p = new Predicate<Integer>(){...
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Sun Dec 02, 2018 8:42 pm
by admin
It is instantiating an anonymous class that implements Predicate interface. The concept of anonymous class is explained in section 9.2.7.
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Fri Dec 07, 2018 11:26 am
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?
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Fri Dec 07, 2018 11:35 am
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);
}
}
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Fri Dec 07, 2018 11:42 am
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.
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Thu May 02, 2019 6:58 pm
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.
Re: [HD Pg 0, Sec. 12.5.4 - using-predicate-with-arraylist]
Posted: Thu May 02, 2019 11:57 pm
by admin
Right.