1

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.

2
  • 2
    Would you be able to answer your own question if you replaced the lambda expression with an anonymous class implementing EmployeeAudit? Commented Jun 11, 2021 at 18:41
  • Always avoid mutating external references in lambda. A better version is: EmployeeAudit aud = sal -> employeeMap.entrySet().stream().filter(e -> sal >= e.getValue()).map(Entry::getKey).collect(Collectors.toList()); Commented Jun 12, 2021 at 14:08

2 Answers 2

4

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

so this overriding of the abstract method happens only in the case of functional interface? So, as much I understand your answer. because our findEmployee() has a return type of interface EmployeeAudit thus it will create an instance of it and thus its member function in this case fetchEmployeeDetails() can be accessed. Now can you explain me a bit more on how lambda function in return statement of findEmployee() overrides the fetchEmployeeDetails()?
@varun Read about "functional interface" like baeldung.com/java-8-functional-interfaces. Any method can defined with a lambda, BUT in case of an interface with a single method can also be defined with a lambda : that is a functional interface
1

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) { ??? }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.