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 List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

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 a clean, readable code. Before java 8, 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 class instance and the using instance, we need to invoke the method or other way was to use anonymous class with corresponding method.

With lambda expression, we can avoid need of both concrete as well as anonymous class object. 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 purpose. 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 functional interface so that 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

There are primarily four types of functional interfaces in Java.

Predicate Functional Interface

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 accepts 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

consumer functional interface is the 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 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 accepts 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

supplier functional interface is the one whose method is not having 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 the 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 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 accepts only Integer, Double and Long respectively. There are two more utility interfaces, UnaryOperator which extends Function interface and BinaryOperator which extends 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