Java 8 : Lambda Expressions and Functional Interfaces

Juhilee NazareJuhilee Nazare
3 min read

Introduction

Java 8 introduced some of the most significant enhancements to Java programming language. These features bought functional programming, stream based data processing, and improved API support into language. In this article, we’ll learn about the Lambda Expressions and Functional Interface in Java 8.

Lambda Expressions

  1. Lambda Expressions are anonymous functions (function with no name) introduced to enable functional programming in Java and improve of readability. But what does it actually mean? Functional Programming (FP) focuses on writing pure functions and immutability of data - what to do and not how to do.

  2. It allows you to treat functions as data, pass functions as arguments, return them from other functions and store them in variables.

For example: To print all elements of list you can write a single line of code as follows.

List<String> names = Arrays.asList("Jui", "Tim", "Bob");
names.forEach(System.out::println);   //short hand method reference in lambda

Question: How does Java interpret these Lambda Expressions ?

Answer: Java is a statically typed language. It makes use of Functional Interface to understand what a lambda expression does, what parameters it takes and what is return type.


Functional Interface

  1. Functional Interface is an interface having exactly one abstract method. Without a Functional Interface java wouldn’t know how to interpret a lambda.
//The lambda expression you write
List<String> names = Arrays.asList("Jui", "Tim", "Bob");
names.forEach(System.out::println);   //short hand method reference in lambda

//Java Interpretation using Consumer Functional Interface
Consumer<String> consumer = new Consumer<String>() {
    @Override
    public void accept(String name) {
        System.out.println("Hello, " + name);
    }
};

Where Consumer is an in-built Functional interface available in Java with one abstract method void accept(T t).

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
    // Default method for chaining consumers  
}

Below are few more commonly used in-built Functional Interfaces

Functional InterfaceAbstract MethodExample
Function<T, R>R apply (T t)x → x * 2
Predicate <T>boolean test (T t)x → x > 10
Supplier<T>T get()() → 40
  1. We can also define custom Functional Interfaces.
@FunctionalInterface       //Annotation is optional but ensures that constraints are followed
interface MyMathInterface {
    int operate(int a, int b);         //Single abstract method
}

public static void main(String[] args) {
    MyMathInterface add = (a, b) -> a + b;
    MyMathInterface multiply = (a, b) -> a * b;

    System.out.println(add.operate(2, 5));     //7
    System.out.println(multiply.operate(2, 5));     //10
}

Question: Can a Functional Interface have multiple default methods?

Answer: Yes, a Functional Interface can have multiple default or static methods, as long as it has only one abstract method.


Question: Can I write a lambda expression without a Functional Interface?

Answer: No, Java requires a Functional Interface context to evaluate a lambda. It uses the interface's single method as the target.


Question: How is functional interface different than normal interface ??

Answer: A normal interface can have any number of abstract methods, and cannot be used directly with lambdas unless it fits the functional interface requirement. A normal interface is used to define contracts with multiple behaviours.


Summary

Java 8 introduced significant enhancements, including Lambda Expressions and Functional Interfaces, which enable functional programming by treating functions as data. Lambda Expressions are anonymous functions used for tasks like list processing, while Functional Interfaces have one abstract method, allowing Java to interpret lambdas. Examples of built-in Functional Interfaces include Consumer, Function, Predicate, and Supplier. Custom Functional Interfaces can also be defined, and they support multiple default methods. However, a Functional Interface requires exactly one abstract method to work with lambdas.

1
Subscribe to my newsletter

Read articles from Juhilee Nazare directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Juhilee Nazare
Juhilee Nazare

I've 3.5+ years of experience in software development and willing to share my knowledge while working on real-time IT solutions.