Java 8 - Functional Interfaces

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.
BiConsumer<T,U>
Represents an operation that accepts two input arguments, and returns no result.
BiFunction<T,U,R>
Represents a function that accepts two arguments and produces a result.
BinaryOperator<T>
Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
BiPredicate<T,U>
Represents a predicate (Boolean-valued function) of two arguments.
BooleanSupplier
Represents a supplier of Boolean-valued results.
Consumer<T>
Represents an operation that accepts a single input argument and returns no result.
DoubleBinaryOperator
Represents an operation upon two double-valued operands and producing a double-valued result.
DoubleConsumer
Represents an operation that accepts a single double-valued argument and returns no result.
DoubleFunction<R>
Represents a function that accepts a double-valued argument and produces a result.
DoublePredicate
Represents a predicate (Boolean-valued function) of one double-valued argument.
DoubleSupplier
Represents a supplier of double-valued results.
DoubleToIntFunction
Represents a function that accepts a double-valued argument and produces an int-valued result.
DoubleToLongFunction
Represents a function that accepts a double-valued argument and produces a long-valued result.
DoubleUnaryOperator
Represents an operation on a single double-valued operand that produces a double-valued result.
Function<T,R>
Represents a function that accepts one argument and produces a result.
IntBinaryOperator
Represents an operation upon two int-valued operands and produces an int-valued result.
IntConsumer
Represents an operation that accepts a single int-valued argument and returns no result.
IntFunction<R>
Represents a function that accepts an int-valued argument and produces a result.
IntPredicate
Represents a predicate (Boolean-valued function) of one int-valued argument.
IntSupplier
Represents a supplier of int-valued results.
IntToDoubleFunction
Represents a function that accepts an int-valued argument and produces a double-valued result.
IntToLongFunction
Represents a function that accepts an int-valued argument and produces a long-valued result.
IntUnaryOperator
Represents an operation on a single int-valued operand that produces an int-valued result.
LongBinaryOperator
Represents an operation upon two long-valued operands and produces a long-valued result.
LongConsumer
Represents an operation that accepts a single long-valued argument and returns no result.
LongFunction<R>
Represents a function that accepts a long-valued argument and produces a result.
LongPredicate
Represents a predicate (Boolean-valued function) of one long-valued argument.
LongSupplier
Represents a supplier of long-valued results.
LongToDoubleFunction
Represents a function that accepts a long-valued argument and produces a double-valued result.
LongToIntFunction
Represents a function that accepts a long-valued argument and produces an int-valued result.
LongUnaryOperator
Represents an operation on a single long-valued operand that produces a long-valued result.
ObjDoubleConsumer<T>
Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
ObjIntConsumer<T>
Represents an operation that accepts an object-valued and an int-valued argument, and returns no result.
ObjLongConsumer<T>
Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
Predicate<T>
Represents a predicate (Boolean-valued function) of one argument.
Supplier<T>
Represents a supplier of results.
ToDoubleBiFunction<T,U>
Represents a function that accepts two arguments and produces a double-valued result.
ToDoubleFunction<T>
Represents a function that produces a double-valued result.
ToIntBiFunction<T,U>
Represents a function that accepts two arguments and produces an int-valued result.
ToIntFunction<T>
Represents a function that produces an int-valued result.
ToLongBiFunction<T,U>
Represents a function that accepts two arguments and produces a long-valued result.
ToLongFunction<T>
Represents a function that produces a long-valued result.
UnaryOperator<T>
Represents an operation on a single operand that produces a result of the same type as its operand.
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:\> JAVA.
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Java8Tester {

   public static void main(String args[]) {
      List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
		
      // 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:");
		
      //pass n as parameter
      eval(list, n->true);
		
      // Predicate<Integer> predicate1 = n -> n%2 == 0
      // n is passed as parameter to test method of Predicate interface
      // test method will return true if n%2 comes to be zero
		
      System.out.println("Print even numbers:");
      eval(list, n-> n%2 == 0 );
		
      // Predicate<Integer> predicate2 = n -> n > 3
      // n is passed as parameter to test method of Predicate interface
      // test method will return true if n is greater than 3.
		
      System.out.println("Print numbers greater than 3:");
      eval(list, n-> n > 3 );
   }
	
   public static void eval(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.
Compile the class using javac compiler as follows −
C:\JAVA>javac Java8Tester.java

Now run the Java8Tester as follows −
C:\JAVA>java Java8Tester

It should produce the following output −
Print all numbers:
1
2
3
4
5
6
7
8
9
Print even numbers:
2
4
6
8
Print numbers greater than 3:
4
5
6
7
8
9


    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page