Java Functional Interfaces for the Impatient: Part 1

Java Functional Interfaces for the Impatient: Part 1

In this article, you will learn about Java Functional Interfaces with coding examples.

Introduction

Functional Interfaces were first introduced in Java 8 and as per Java docs:

Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression’s parameter and return types are matched or adapted.

In Java, a functional interface is a type of interface that represents a single functionality, or action, that can be performed in your program. Think of it as a recipe describing how to perform a specific action, like baking a cake. The recipe gives step-by-step instructions on what to do but doesn’t do the action for you. Similarly, a functional interface in Java defines a method or set of methods describing a specific action your program can perform.

Functional interfaces are helpful because they can simplify and streamline your code. For example, let’s say you have a program that needs to perform many similar actions, like adding two numbers together or converting a string to uppercase. Instead of writing out the code for each action separately, you can define a functional interface that describes the action and then use that interface to act as many times as needed.

Why were Functional Interfaces added to Java?

Functional interfaces were added to Java to support better a programming style called functional programming, which emphasizes using functions as objects that can be passed around and used as inputs or outputs to other functions. In other words, you can treat a function as a variable, pass it as an argument to other methods, or assign it to a variable, just like you would with a regular value. This makes writing more modular and flexible code easier, which can be easier to read and maintain.

Before Java 8, creating functions in Java was more complicated and required more code, which made functional programming in Java less intuitive and efficient.

Classification

Functional Interfaces under java.util.function package can be broadly classified into the following.

  1. Consumer <T>

  2. Function<T,R>

  3. Supplier<T>

  4. Predicate<T>

Consumer<T> Functional Interface

A Consumer is a functional interface with a single abstract method accept(Object), which allows only one argument and returns no results.

import java.util.function.Consumer;

public class ConsumerExample {
    public static void main(String[] args) {
        Consumer<String> printValue = s -> System.out.println(s);
        printValue.accept("Hello World");
    }
}

Predicate<T> Functional Interface

A Predicate is a functional interface representing a function that takes one argument and returns a boolean value. It is commonly used for filtering, testing, and validation of objects.

import java.util.function.Predicate;

public class PredicateExample {
    public static void main(String[] args) {
        Predicate<String> stringLength = s -> s.length() > 5;
        System.out.println(stringLength.test("Hello World")); //true
        System.out.println(stringLength.test("Java")); //false
    }
}

Function<T,R> Functional Interface

A Function is a functional interface representing a function that takes one argument and returns a value. It is frequently used for mapping or transforming input values to output values.

import java.util.function.Function;

public class FunctionExample {
    public static void main(String[] args) {
        Function<String, Integer> stringToInt = s -> Integer.parseInt(s);
        System.out.println(stringToInt.apply("10")); //10
    }
}

Supplier<T> Functional Interface

A Supplier is a functional interface representing a function that takes no arguments and returns a value. It is typically used for lazy evaluation of objects or for generating values.

import java.util.function.Supplier;

public class SupplierExample {
    public static void main(String[] args) {
        Supplier<String> helloSupplier = () -> "Hello World";
        System.out.println(helloSupplier.get());
    }
}

Conclusion

Functional interfaces are a way of describing specific actions that your program can perform and then using those descriptions to simplify and streamline your code. They’re a powerful tool to help you write more efficient and flexible code. By mastering these interfaces, developers can create more efficient, concise, and effective code.

Further Reading

Did you find this article valuable?

Support Ashish Choudhary by becoming a sponsor. Any amount is appreciated!