Java Predicates , Function and Lambda expressions

Like function pointers in C , we have a powerful tool in Java. We can easily extend our application by using this powerfull tools in Java. For instance, let us assume we are developing an image edit tool in Java. And we want to provide a extensible base code to other developers . We can always use Interfaces for this purpose , but on the other hand it is nice to know that we can also java.util.function package for this purpose. Basically function is using for function that takes parameter and return parameter. We simply define them as

Function<T,R> function (T t) -> { do something with t and return (R) }

In below code you can find a simple demo code that use Lambda , Predicators and Function in Java.

package com.company;

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

class Employee {
    int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    String name;
    public Employee(int age, String name ) {
        this.age = age;
        this.name = name;
    }
}

public class Main {

    public static void main(String[] args) {

        Employee emp1 = new Employee(15, "Ozan");
        Employee emp2 = new Employee(25, "Kemal");
        Employee emp3 = new Employee(35, "Zeynep");
        Employee emp4 = new Employee(45, "Enes");
        Employee emp5 = new Employee(55, "Mustafa");

        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(emp1);
        employees.add(emp2);
        employees.add(emp3);
        employees.add(emp4);
        employees.add(emp5);

        // we can use lambda expression in foreach loop
        employees.forEach(employee -> {
            if(employee.getAge() > 4) {
                System.out.println(employee.getName() + " is  greater than " + 4);
            }
        });

        // we can use anonymous class as Predicate
        printEmploye(employees, " older than 25 ", new Predicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return (employee.getAge() > 25);

            };
        });


        // we can use lambda expression as predicate
        printEmploye(employees , "lower than 25 " , employee -> employee.getAge() < 25);
        printEmploye(employees , "lower than 20" , e-> e.getAge() < 20);


        Function<Employee,String> getEmployeProp = (Employee employe)-> {
            return employe.getName();
        };

        // we can use functions as parameter to another method.
        returnEmployeeProp(employees,getEmployeProp);
        // we can use lambda expression as function
        returnEmployeeProp(employees, e->{
            return e.getName();
        });

        IntPredicate greaterThan15 = i-> i > 15;
        IntPredicate lessThan100 = i-> i < 100;

        // we can chain different Predicates.
        System.out.println(greaterThan15.and(lessThan100).test(25));

	// write your code here
    }


    private static void printEmploye(List<Employee> employeeList,
                                String ageText,
                                Predicate<Employee> ageCondition) {
        for (Employee emp: employeeList) {
            if(ageCondition.test(emp)) {
                System.out.println(ageText + " " + emp.getName());
            }
        }

    }

    private static ArrayList<String> returnEmployeeProp(List<Employee> employeeList , Function<Employee,String> propFnc) {
        ArrayList<String> employesProp = new ArrayList<>();
        employeeList.forEach(e->{
            employesProp.add(propFnc.apply(e));
        });
        return employesProp;
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *