The function type functional interface receives a single argument, processes it, and returns a value. Functional interface is taking the key from the user as input and searching for the value in the map for the given key. details..
public static void main (
String
[] args) { private static HashMap<
Integer
,
String
> employeeMap = new HashMap<>(); Function<
Integer
,
String
> addFunc = (Integer ID)-> { if(employeeMap.containsKey(ID)) { return employeeMap.get(ID); }else{ return "Invalid ID"; } }; // Returns the Employee object if present in the hashmap or return Invalid Id message System.out.println( addFunc.apply( 500 )); };
import java.util.HashMap; import java.util.function.Function; public class EmployeeFunctionExample { private static HashMap<
Integer, String
> employeeMap = new HashMap<>(); static Function<
Integer, String
> addFunc = (Integer ID) -> { if (employeeMap.containsKey(ID)) { return employeeMap.get(ID); } else { return "Invalid ID"; } }; public static void main(String args[]) { employeeMap.put(100, "Employee1"); employeeMap.put(200, "Employee1"); employeeMap.put(300, "Employee1"); System.out.println(addFunc.apply(500)); System.out.println(addFunc.apply(100)); } } Output: Invalid ID Employee1
Bi-function is just like a function except it takes two arguments. Two arguments are must in Bi-function. Just like a function it also returns a value.