Function Functional Interface

less than 1 minute read

Function

The Function interface represents a function that takes a single parameter T and returns a single value R.

Commonly used with streams.map(). A map applies a given function to each element of a list stream returning a list of results in the same order.

public interface Function<T, R> {
    R apply(T t);
}

Write Lambda in such a way that it **accepts an argument and performs an action ** on it to return an Object

BiFunction

Takes in Two parameters T & U and return a single value R

public interface BiFunction<T, U, R> {
     R apply(T t, U u);
}

BiFunction can be defined as

BiFunction<Integer, Double, String> biFunction = (x, y) -> String.valueOf(Math.hypot(x, y));
System.out.println(biFunction.apply(3,4.0));//Returns answer in string format

Tags:

Categories:

Updated: