I am using predicates supported in Java 8, in which I am using lambdas, but I want to use method references in place of lambdas. Please advise how to achieve this. Below is my code.
public class Employee {
public Employee(Integer id, Integer age, String gender, String fName, String lName){
this.id = id;
this.age = age;
this.gender = gender;
this.firstName = fName;
this.lastName = lName;
}
private Integer id;
private Integer age;
private String gender;
private String firstName;
private String lastName;
//Please generate Getter and Setters
@Override
public String toString() {
return this.id.toString()+" - "+this.age.toString(); //To change body of generated methods, choose Tools | Templates.
}
And here I am using predicates with lambdas where I want to remove lambdas and use method references instead:
public class EmployeePredicates
{
public static Predicate<Employee> isAdultMale() {
return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");
}
public static Predicate<Employee> isAdultFemale() {
return p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("F");
}
public static Predicate<Employee> isAgeMoreThan(Integer age) {
return p -> p.getAge() > age;
}
public static List<Employee> filterEmployees (List<Employee> employees, Predicate<Employee> predicate) {
return employees.stream().filter( predicate ).collect(Collectors.<Employee>toList());
}
}