Function identity returns a function that always returns its input argument.
identity
has the following syntax.
static <T> Function<T,T> identity()
The following example shows how to use identity
.
import java.util.function.Function; /*w w w . ja v a 2 s. co m*/ public class Main { public static void main(String[] args) { Function<Integer,Integer> id = Function.identity(); System.out.println(id.apply(3)); } }
The code above generates the following result.
The following code shows how to use Function.identity() to not modify values.
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; /* w ww. ja va 2 s .c o m*/ public class Main { public static void main(String[] args) { Function<Double, Double> square = number -> number * number; Function<Double, Double> half = number -> number * 2; List<Double> numbers = Arrays.asList(10D, 4D, 12D); // you can use identity to not modify them System.out.println(mapIt(numbers, Function.<Double>identity())); } private static List<Double> mapIt(List<Double> numbers, Function<Double, Double> fx) { List<Double> result = new ArrayList<>(); for (Double number : numbers) { result.add(fx.apply(number)); } return result; } }
The code above generates the following result.