Java defines many functional interfaces in java.util.function
packages:
Interface | Purpose |
---|---|
UnaryOperator<T> | Apply a unary operation to an object of type T and return the result, which is of type T. Its method is called apply (?). |
BinaryOperator<T> | Apply an operation to two objects of type T and return the result, which is of type T. Its method is called apply(?). |
Consumer<T> | Apply an operation on an object of type T. Its method is called accept(?). |
Supplier<T> | Return an object of type T. Its method is called get(?). |
Function<T, R> | Apply an operation to an object of type T and return the result as an object of type R. Its method is called apply(?). |
Predicate<T> | Determine if an object of type T fulfills some constraint. Its method is called test(?). |
import java.util.function.Function; public class Main { public static void main(String args[]) { Function<Integer, Integer> factorial = (n) -> { int result = 1; for (int i = 1; i <= n; i++) { result = i * result;//from w w w .j ava2s . c o m } return result; }; System.out.println("The factoral of 3 is " + factorial.apply(3)); System.out.println("The factoral of 5 is " + factorial.apply(5)); } }