Here java 8 method as argument example or example of functional interface.

FuncationDemo.java

package com.javadeveloperzone;


import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Created by JavaDeveloperZone on 30-04-2017.
 */
public class FunctionDemo {
    public static void main(String... args) {
        java.util.List employees = Employee.getEmployee();

        System.out.println("Print all male employee");
        getEmployee(employees, employee ->
             employee.getGender().equals("Male")
        ).forEach(System.out::println);


        System.out.println("Print all female employee");
        getEmployee(employees, employee ->
                employee.getGender().equals("Female")
        ).forEach(System.out::println);


        System.out.println("Print all male and developer employee");
        getEmployee(employees, employee ->
                employee.getGender().equals("Male") && employee.getDesignation().equals("Developer")
        ).forEach(System.out::println);

    }

    public static java.util.List<Employee> getEmployee(java.util.List<Employee> employees, Predicate<Employee> employeePredicate) {
        return employees.stream().filter(employeePredicate).collect(Collectors.toList());
    }
}

 Employee.Java

package com.javadeveloperzone;

import java.util.ArrayList;

/**
 * Created by JavaDeveloperZone on 29-04-2017.
 */
public class Employee {
    private int no;
    private String name;
    private String designation;
    private String gender;

    public Employee(int no, String name, String designation, String gender) {
        this.no = no;
        this.name = name;
        this.designation = designation;
        this.gender = gender;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return this.no + " : " + this.name + " : " + gender + " : " + designation;
    }

    public static java.util.List<Employee> getEmployee() {
        java.util.List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "Bob", "Developer", "Male"));
        employees.add(new Employee(2, "Joy", "Sr. Developer", "Male"));
        employees.add(new Employee(3, "John", "CEO", "Male"));
        employees.add(new Employee(4, "Bat", "Developer", "Male"));
        employees.add(new Employee(5, "Jolly", "Developer", "Female"));
        employees.add(new Employee(6, "Bobby", "Developer", "Female"));
        return employees;
    }
}

 Output:

Print all male employee
1 : Bob : Male : Developer
2 : Joy : Male : Sr. Developer
3 : John : Male : CEO
4 : Bat : Male : Developer
Print all female employee
5 : Jolly : Female : Developer
6 : Bobby : Female : Developer
Print all male and developer employee
1 : Bob : Male : Developer
4 : Bat : Male : Developer

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

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