Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Functional Interfaces



Functional interfaces were introduced in Java 8 along with lambda expression and method references. These three features were added to boost functional programming in Java and to write clean, readable code. Before Java 8, a lot of boilerplate code had to be written to cover basic functionality. For example, in order to call a function, first we had to create a class with the required method, create a class instance, and the instance, needed to invoke the method or another way was to use an anonymous class with the corresponding method.

With lambda expression, we can avoid the need for both concrete as well as anonymous class objects. A functional interface assists one step ahead, as a lambda expression can implement the functional interface easily as only one method is to be implemented.

Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method compareTo() is used for comparison purposes. But it can have any number of default and static methods.

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.

@FunctionalInterface Annotation

By functionality, any interface having a single abstract method is a functional interface. Java provides a @FunctionalInterface annotation to mark an interface as a functional interface so that the compiler can check if an interface is a functional interface or not. This annotation is optional and is primarily to add a compiler check and to increase the code readability and maintenance.

Types of Functional Interfaces in Java

There are primarily four types of functional interfaces in Java.

Predicate Functional Interface

A predicate functional interface is the one whose method accepts one argument and will return either true or false. A predicate functional interface is mainly used in comparison to sort elements or to filter a value based on certain condition(s) applied on the input passed. Java provides predicate functional interfaces for primitives as well as IntPredicate, DoublePredicate, and LongPredicate, which accept only Integer, Double, and Long respectively.

Usage

Predicate predicate = (value) -> value  != 0;
// or
Predicate predicate = (value) -> test(value);

In above snippet predicate function is to return either true/false based on the value passed.

Example

In this example, we’re using a predicate functional interface to filter odd numbers from a list of integers with the help of a lambda expression.

package com.tutorialspoint;

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

public class Tester {
   public static void main(String args[]) {
      List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8);

      Predicate<Integer> isEvenNumber = n -> n %2 == 0;
      numbers =  numbers.stream().filter(isEvenNumber).toList();

      System.out.println(numbers);
   }
}

Let us compile and run the above program, this will produce the following result −

[2, 4, 6, 8]

Consumer Functional Interface

A consumer functional interface is one whose method accepts one argument and will not return anything. A consumer functional interface is mainly used for side-effect operations. For example, to print an element, to add a salutation, etc. There are other variants of Consumer as well like BiConsumer. A BiConsumer functional interface can accept two parameters. Java provides consumer functional interfaces for primitives as well as IntConsumer, DoubleConsumer, and LongConsumer, which accept only Integer, Double, and Long respectively.

Usage

Consumer consumer = (value) -> System.out.println(value);
// Or
Consumer consumer1 = System.out::println;
// Or
Consumer consumer2 = (value) -> accept(value);

Example

In this example, we’re using consumer functional interface to print all numbers from a list of integers with the help of a lambda expression and a method reference.

package com.tutorialspoint;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class Tester {
   public static void main(String args[]) {
      List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8);

      Consumer<Integer> consumer = (value) -> System.out.println(value);
      Consumer consumer1 = System.out::println;

      System.out.println("Printing using consumer functional interface as lambda expression");
      numbers.forEach(consumer);

      System.out.println("Printing using consumer functional interface as method reference");
      numbers.forEach(consumer1);
   }
}

Let us compile and run the above program, this will produce the following result −

Printing using consumer functional interface as lambda expression
1
2
3
4
5
6
7
8
Printing using consumer functional interface as method reference
1
2
3
4
5
6
7
8

Supplier Functional Interface

The supplier functional interface is the one whose method does not have any arguments to pass and will return a value. A supplier functional interface is mainly used to generate values lazily. For example, to get a random number, to generate a sequence of numbers, etc.

Usage

Supplier supplier = () -> Math.random() * 10;
// or
Supplier supplier1 = () -> get();

Example

In this example, we’re using supplier functional interface to get a random number with the help of a lambda expression.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class Tester {
   public static void main(String args[]) {
      Supplier<Integer> supplier = () -> (int)(Math.random() * 10);

      List<Integer> randomNumbers = new ArrayList<>();

      // generate 10 random numbers
      for(int i = 0; i< 10; i++) {
         randomNumbers.add(supplier.get());
      }
      System.out.println(randomNumbers);
   }
}

Let us compile and run the above program, this will produce the following result −

[0, 8, 8, 8, 8, 5, 7, 5, 5, 9]

Function Functional Interface

Function functional interface is one whose method accepts one argument and will return a value. A function functional interface is mainly used to get the processed value. For example, to get the square of an element, to trim string values, etc. There are other variants of Function as well like BiFunction. A BiFunction functional interface can accept two parameters. Java provides function functional interfaces for primitives as well as IntFunction, DoubleFunction, and LongFunction, which accept only Integer, Double, and Long respectively. There are two more utility interfaces, UnaryOperator which extend the Function interface, and BinaryOperator which extends the BiFunction interface.

Usage

Function function = (value) -> Math.random() * 10;
// or
Function function1 = (value) -> apply(value);

Example

In this example, we’re using function functional interface to get a list of squares of numbers with the help of a lambda expression.

package com.tutorialspoint;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class Tester {
   public static void main(String args[]) {
      List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8);

      Function<Integer, Integer> squared = (value) -> value * value;  

      List<Integer> squaredNumbers =  numbers.stream().map(squared).toList();

      System.out.println(squaredNumbers);
   }
}

Let us compile and run the above program, this will produce the following result −

[1, 4, 9, 16, 25, 36, 49, 64]

Existing Functional Interfaces Prior to Java 8

With Java, many existing interfaces are annotated as functional interfaces and can be used in lambda expressions. For example:

  • Runnable − provides run() method

  • Callable − provides call() method

  • Actionlistener − provides actionPerformed() method

  • Comparable − provides compareTo() method to compare two numbers

Example

In this example, we’re creating two threads. First is created using anonymous class and second is using lambda expression. Both are using runnable interface to create the thread instance.

package com.tutorialspoint;

public class Tester {
   public static void main(String args[]) {
      // create anonymous inner class object
      new Thread(new Runnable() {
         @Override public void run() {
            System.out.println("Thread 1 is running");
         }
      }).start();

      // lambda expression to create the object
      new Thread(() -> {
         System.out.println("Thread 2 is running.");
      }).start();   
   }
}

Let us compile and run the above program, this will produce the following result −

Thread 1 is running
Thread 2 is running.
Advertisements