简体   繁体   中英

java functional interface and lambda expression

functional interface

import java.util.ArrayList;
public interface EmployeeAudit {

    public ArrayList<String> fetchEmployeeDetails (double salary);
}

in public class Main

public static EmployeeAudit findEmployee(){
    ArrayList<String> name=new ArrayList<>();
    return  (sal) -> {
        employeeMap.forEach((key,value) -> {
            if(value<=sal) 
                name.add(key);
        });
        return name;
    };
}    

In main function:

ArrayList<String> str = findEmployee().fetchEmployeeDetails(sal);

can anyone help me to understand how value of sal is transferred to findEmployee(), as findEmployee() is called first as per the chaining. And how these calls are working.

Your method findEmployee() does in fact instanciate an EmployeeAudit object. EmployeeAudit is an interface so it needs to define its method, as their is only one, it is a functionnal interface and can be done with a lambda but thta is equivalent to

public static EmployeeAudit findEmployee() {
    ArrayList<String> name = new ArrayList<>();
    return new EmployeeAudit() {
        @Override
        public ArrayList<String> fetchEmployeeDetails(double sal) {
            employeeMap.forEach((key, value) -> {
                if (value <= sal)
                    name.add(key);
            });
            return name;
        }
    };
}

Then, on that instance, you call the fetchEmployeeDetails method, and that is maybe easier to see with splitting the code

EmployeeAudit ea = findEmployee();
ArrayList<String> str = ea.fetchEmployeeDetails(10);

You could even imagine create the class implementing EmployeeAudit and use very easily

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

interface EmployeeAudit {
    ArrayList<String> fetchEmployeeDetails(double salary);
}

public class Test {
    static Map<String, Integer> employeeMap = new HashMap<>();

    public static void main(String[] args) {
        employeeMap.put("Jean", 10);
        employeeMap.put("Jean2", 100);
        employeeMap.put("Jean3", 100);
        EmployeeAudit ea = new EmployeeAuditImpl();
        ArrayList<String> str = ea.fetchEmployeeDetails(10);
        System.out.println(str);
    }

    static class EmployeeAuditImpl implements EmployeeAudit {
        @Override
        public ArrayList<String> fetchEmployeeDetails(double sal) {
            ArrayList<String> name = new ArrayList<>();
            employeeMap.forEach((key, value) -> {
                if (value <= sal)
                    name.add(key);
            });
            return name;
        }
    }
}

findEmployee() returns an anonymous function (actually a EmployeeAudit defined as a function because EmployeeAudit is a functional interface).

This function is defined as taking an input parameter called sal but this is not the sal of your main call: you could rename sal to anything else in findEmployee and the code would work the same.

The inner sal is the name of a parameter like you could use in any other regular function:

void someFunction(double sal) { ??? }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM