Java 8: Built-In Functional Interfaces. Supplier, Function, Predicate

Functional Interface was added in Java 8. Functional Interface is like a normal interface with only one abstract method. Remember, a Functional interface can have a default method but should have only one abstract method which is unimplemented. Also, @FunctionalInterface annotation is optional to define the functional interface.

Example for Functional Interface:

				
					@FunctionalInterface   //It is optional
interface PrintMessage{
    public void message(String text);
}
				
			

Table of Contents

Built-In Functional Interfaces

java.util.function package has many functional interfaces. However below are the most popular used:

Function Interface

Function interface is a Functional Interface and it has only one abstract method called apply(). Below is the apply() method signature.

T – Type of the Input parameter

R – Type of the return value.

				
					public R apply(T t);
				
			

Function interface also has few default methods andThen(), compose() and one static method identity().

				
					default <V> Function<T,V> andThen(Function<? super R,? extends V> after);

default <V> Function<V,R> compose(Function<? super V,? extends T> before)

static <T> Function<T,T> identity();
				
			

Example: Find the length of String using Function Interface

Here Function<String, Integer>, the first type String is for input type and second type Integer is for return type for apply() method.

				
					public class FunctionExample
{
    public static void main(String[] args) {
        Function<String,Integer> length = (a)->{
            return a.length();
        };
        System.out.println("Length is : "+length.apply("Function interface"));
    }
}
				
			

Output:

				
					Length is : 18

				
			

What is andThen() Method?

In Function Interface, apply() will execute the implementation given in the lambda expression. And If we have multiple Function Interfaces and we want to execute those Function Interfaces in particular sequence where the output of one function can be input to the other function and at the end you get the output.

Example: Find the length of String using Function Interface with andThen() method

				
					public class FunctionExample
{
        // Function to calculate the length
        Function<String,Integer> length = (a)->a.length();
        
        // Function to format the result
        Function<Integer,String> format = (a)-> "Length is " + a;
      
        //Store the final result
        String output = length.andThen(format).apply("Function interface");
        
        System.out.println(output);
}
				
			

Output:

				
					Length is : 18

				
			

What is compose() Method?

compose() method is also similar to andThen() method but it works a bit differently. compose() method accepts another function variable as a parameter. 

Example: 

Function<T,R> fun1 = (a)->a;

Function<T,R> fun2 = (b)->b;

Let’s say fun1.compose(fun2) in this statement we are calling the compose() method on fun1 and it accepts the other function fun2 as an input. While executing this statement, first fun2.apply() will be performed and the output of this will be passed to fun1. It is just the opposite of how andThen() method works. 

Example: Find the length of String using Function Interface with compose() method

				
					public class FunctionExample
{
        // Function to calculate the length
        Function<String,Integer> length = (a)->a.length();
        
        // Function to format the result
        Function<Integer,String> format = (a)-> "Length is " + a;
      
        //Store the final result
        String output = format.compose(length).apply("Function interface");
        
        System.out.println(output);
}
				
			

Output:

				
					Length is : 18

				
			

What is identity() Method?

identity() method has a very simple task. it just returns the value of the input. if you call Function.identity() it will return the same value which you gave in input. other than this it doesn’t have any other task.

				
					public class FunctionExample
{
    Function<String,String> value = Function.identity();
    System.out.println(value.apply("Test"));
}
				
			

Output:

				
					Test

				
			
Java 8 Stream APIs

Java 8: Introduction to Streams API

Java 8 got a brand new API called streams. it makes every developer’s life easy. Stream APIs are used to process the group of data. mostly streams are applied on collection objects but we can use stream with any group

Read More »