Predefined Functional Interface - Function-Java
Predefined Functional Interface Functions are exactly same as predicates except that functions can return any type of result but function should (can) return only one value and that value can be any type as per our requirement. To implement functions oracle people introduced Function interface in 1.8version. Function interface present in Java.util.function package. Functional interface contains only one method i.e., apply() interface function(T,R) { public R apply(T t); } Assignment: Write a function to find length of given input string. Ex: import Java.util.function.*; class Test { public static void main(String[] args) { Function<String, Integer> f = s ->s.length(); System.out.println(f.apply("Hello MS")); System.out.println(f.apply("JDK8")); } } Note: Function is a functional interface and hence it can refer lambda expression. Differences between predicate and function Predicate Function ...