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

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

RDS vs Aurora DB

Junit Mockito and Power Mockito