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;
}
}
}
EmployeeAudit?EmployeeAudit aud = sal -> employeeMap.entrySet().stream().filter(e -> sal >= e.getValue()).map(Entry::getKey).collect(Collectors.toList());