package com.richard.lambdaexpressions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
public class Roster2 {
public static void printPersonsWithPredicate(List<Person> roster,Predicate<Person> tester){
for(Person p:roster){
if(tester.test(p)){
p.printPerson();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// List<String> ls=new ArrayList<String>();
// List<Object> lo=ls;
// Collection<String> c = new ArrayList<String>();
// c.add("hello"); // Compile time error
List<Person> roster=Person.createRoster();
printPersonsWithPredicate(roster,
p->p.getGender()==Person.Sex.FEMALE &&
p.getAge()>=30 &&
p.getAge()<=40);
printPersonsWithPredicate(roster,
p->p.getGender()==Person.Sex.MALE &&
p.getAge()>=20 &&
p.getAge()<=50);
}
}
网友评论