Java 8 Function
Function
A Function interface is more of a generic one that takes one argument and produces a result. This has a Single Abstract Method (SAM) apply which accepts an argument of a type T and produces a result of type R. One of the common use cases of this interface is Stream.map method. This is shown as below:
@Test
public void testFunctions(){
List<String> names = Arrays.asList("Smith", "Gourav", "Heather", "John", "Catania");
Function<String, Integer> nameMappingFunction = String::length;
List<Integer> nameLength = names.stream().map(nameMappingFunction).collect(Collectors.toList());
System.out.println(nameLength);
Comments
Post a Comment