Java 8

Java8 – Functional Interface

Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. Following is the list of functional interfaces defined in java.util.Function package.

Given below is the list of interfaces in Java8.

Sr.No.Interface & Description
1BiConsumer<T,U>Represents an operation that accepts two input arguments, and returns no result.
2BiFunction<T,U,R>Represents a function that accepts two arguments and produces a result.
3BinaryOperator<T>Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
4BiPredicate<T,U>Represents a predicate (Boolean-valued function) of two arguments.
5BooleanSupplierRepresents a supplier of Boolean-valued results.
6Consumer<T>Represents an operation that accepts a single input argument and returns no result.
7DoubleBinaryOperatorRepresents an operation upon two double-valued operands and producing a double-valued result.
8DoubleConsumerRepresents an operation that accepts a single double-valued argument and returns no result.
9DoubleFunction<R>Represents a function that accepts a double-valued argument and produces a result.
10DoublePredicateRepresents a predicate (Boolean-valued function) of one double-valued argument.
11DoubleSupplierRepresents a supplier of double-valued results.
12DoubleToIntFunctionRepresents a function that accepts a double-valued argument and produces an int-valued result.
13DoubleToLongFunctionRepresents a function that accepts a double-valued argument and produces a long-valued result.
14DoubleUnaryOperatorRepresents an operation on a single double-valued operand that produces a double-valued result.
15Function<T,R>Represents a function that accepts one argument and produces a result.
16IntBinaryOperatorRepresents an operation upon two int-valued operands and produces an int-valued result.
17IntConsumerRepresents an operation that accepts a single int-valued argument and returns no result.
18IntFunction<R>Represents a function that accepts an int-valued argument and produces a result.
19IntPredicateRepresents a predicate (Boolean-valued function) of one int-valued argument.
20IntSupplierRepresents a supplier of int-valued results.
21IntToDoubleFunctionRepresents a function that accepts an int-valued argument and produces a double-valued result.
22IntToLongFunctionRepresents a function that accepts an int-valued argument and produces a long-valued result.
23IntUnaryOperatorRepresents an operation on a single int-valued operand that produces an int-valued result.
24LongBinaryOperatorRepresents an operation upon two long-valued operands and produces a long-valued result.
25LongConsumerRepresents an operation that accepts a single long-valued argument and returns no result.
26LongFunction<R>Represents a function that accepts a long-valued argument and produces a result.
27LongPredicateRepresents a predicate (Boolean-valued function) of one long-valued argument.
28LongSupplierRepresents a supplier of long-valued results.
29LongToDoubleFunctionRepresents a function that accepts a long-valued argument and produces a double-valued result.
30LongToIntFunctionRepresents a function that accepts a long-valued argument and produces an int-valued result.
31LongUnaryOperatorRepresents an operation on a single long-valued operand that produces a long-valued result.
32ObjDoubleConsumer<T>Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
33ObjIntConsumer<T>Represents an operation that accepts an object-valued and an int-valued argument, and returns no result.
34ObjLongConsumer<T>Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
35Predicate<T>Represents a predicate (Boolean-valued function) of one argument.
36Supplier<T>Represents a supplier of results.
37ToDoubleBiFunction<T,U>Represents a function that accepts two arguments and produces a double-valued result.
38ToDoubleFunction<T>Represents a function that produces a double-valued result.
39ToIntBiFunction<T,U>Represents a function that accepts two arguments and produces an int-valued result.
40ToIntFunction<T>Represents a function that produces an int-valued result.
41ToLongBiFunction<T,U>Represents a function that accepts two arguments and produces a long-valued result.
42ToLongFunction<T>Represents a function that produces a long-valued result.
43UnaryOperator<T>Represents an operation on a single operand that produces a result of the same type as its operand.

Functional Interface Example

Predicate <T> interface is a functional interface with a method test(Object) to return a Boolean value. This interface signifies that an object is tested to be true or false.

Create the following Java program using any editor of your choice in, say, C:\hitechpoints\workspace\java8\src>.

Java8Fearure.java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Java8Fearure {

	public static void main(String args[]) {
	      List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
	         
	      //1. Predicate<Integer> predicate = n -> true
	      // n is passed as parameter to test method of Predicate interface
	      // test method will always return true no matter what value n has.	         
	      System.out.println("Print all numbers:");
	      evaluate(list, n -> true);
	         
	      //2. Predicate<Integer> predicate1 = n -> n%3 == 0
	      // n is passed as parameter to test method of Predicate interface
	      // test method will return true if n%3 comes to be zero	         
	      System.out.println("Print which numbers are divided by 3:");
	      evaluate(list, n -> n % 3 == 0 );
	         
	      //3. Predicate<Integer> predicate2 = n -> n > 5
	      // n is passed as parameter to test method of Predicate interface
	      // test method will return true if n is greater than 5.        
	      System.out.println("Print numbers greater than 5:");
	      evaluate(list, n -> n > 5 );
	 }
	     
	 public static void evaluate(List<Integer> list, Predicate<Integer> predicate) {
	     for(Integer n: list) {
	         if(predicate.test(n)) {
	            System.out.println(n + " ");
	         }
	     }
	 }
	   
}

Here we’ve passed Predicate interface, which takes a single input and returns Boolean.

Output

Compile the class using javac compiler as follows −

C:\hitechpoints\workspace\java8\src>javac Java8Fearure.java

Now run the Java8Tester as follows −

C:\hitechpoints\workspace\java8\src>java Java8Fearure 

It should produce the following output −

Print all numbers:
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9
Print which numbers are divided by 3:
 3 
 6 
 9
Print numbers greater than 5:
 6 
 7 
 8 
 9

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs