Tuesday, January 26, 2016

Java Function Programming - Predicate

As java 8 introduces the predicate functions, but we may wonder where to use those true/false returning function in our daily programming life. The answer is we can use this in the following scenario:
1. when we have to evaluate the objects from the same class which will result in the true or false.
2. to filter the objects by using some of its variables etc.

lets hack Predicate with some examples:

A. Examining if the object is the intance of the particular class or not: 

/** * Created by yubraj on 1/26/16. */

public class MyClass {
    Predicate<MyClass> predicate1 = (MyClass myclass) -> this.equals(myclass);
    Predicate<MyClass> predicate2 =  this::equals;
    String text1 = "yubraj", text2 = "pokharel";
    public MyClass(String text1, String text2) {
        this.text1 = text1;        this.text2 = text2;    }

    @Override    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyClass myClass = (MyClass) o;
        if (!text1.equals(myClass.text1)) return false;
        return text2.equals(myClass.text2);
    }

    @Override    public int hashCode() {
        int result = text1.hashCode();      
        result = 31 * result + text2.hashCode();
        return result;    }

    public void myMethod(MyClass cl){
        System.out.println("Intance equals : " + predicate2.test(cl));    }

    public static void main(String[] args) {
        MyClass mc = new MyClass("sudhan", "pokharel");
        MyClass mc2 = new MyClass("yubraj", "pokharel");
        mc.myMethod(mc);
        System.out.println("-----------------------------------");
        mc2.myMethod(mc2);
    }

}

Output:
true
true
//because both are the intance of the similar object.



B. Filtering the objects

for male employee who are older then 21 years

lets add this in the Employee Class

public static Predicate<Employee> isAdultMale() {
    return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");
}

then we can access all the employee who are Male and also more then 21 years by passing method as the variable eg:
public static void main(String[] args){
        Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
        Employee e2 = new Employee(2,13,"F","Martina","Hengis");
        Employee e3 = new Employee(3,43,"M","Ricky","Martin");
        Employee e4 = new Employee(4,26,"M","Jon","Lowman");
        Employee e5 = new Employee(5,19,"F","Cristine","Maria");
        Employee e6 = new Employee(6,15,"M","David","Feezor");
        Employee e7 = new Employee(7,68,"F","Melissa","Roy");
        Employee e8 = new Employee(8,79,"M","Alex","Gussin");
        Employee e9 = new Employee(9,15,"F","Neetu","Singh");
        Employee e10 = new Employee(10,45,"M","Naveen","Jain");
         
        List<Employee> employees = new ArrayList<Employee>();
        employees.addAll(Arrays.asList(new Employee[]{e1,e2,e3,e4,e5,e6,e7,e8,e9,e10}));
                
        System.out.println(filterEmployees(employees, isAdultMale()));
}

Happy Coding :)

References: howtodoinjava, oracle

No comments:

Post a Comment