Skip to content

What is a Supplier Interface in Java?

This lesson talks about the second functional interface, which is the Supplier.

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

What is the Supplier interface?

It is the opposite of the Consumer interface. It takes an input and returns a value, so it supplies a value.

The Supplier interface is a functional interface defined in the java.util.function package. It represents a supplier of values, meaning it does not take any arguments but produces a result of a specified type.

The Supplier interface is often used in scenarios where you need to generate or provide values on demand.

The Supplier interface declares a single abstract method called get(), which has no arguments and returns a value of the specified type. Here's the method signature of the get() method:

Syntax:

T get();

The type parameter T represents the type of the value that the Supplier produces. It can be any valid Java type, including primitive types and objects.

To use the Supplier interface, you can create an instance of it using a lambda expression or method reference that implements the get() method.

Examples

Generate random number

Here's an example that demonstrates how to use a Supplier to generate random integers:

import java.util.Random;
import java.util.function.Supplier;

public class SupplierInterface {
  public static void main(String[] args) {
    Supplier<Integer> randomNumberSupplier =
        () -> {
          Random random = new Random();
          return random.nextInt(100);
        };

    // Calling the get() method to get a random number
    int randomNumber = randomNumberSupplier.get();
    System.out.println("Random number: " + randomNumber);
  }
}

In the example, we create a Supplier<Integer> using a lambda expression that generates a random integer between 0 and 99 (inclusive) using the Random class. Then, we call the get() method on the Supplier instance to obtain a random number.

The Supplier interface is commonly used in functional programming, particularly in situations where lazy evaluation or deferred execution of a value is required. It provides a simple and flexible way to generate values on demand without the need for explicit method arguments.

Note that the Supplier interface is part of the Java 8 functional interfaces introduced to support lambda expressions and functional programming constructs. It is widely used in conjunction with other functional interfaces like Consumer, Predicate, and Function to build powerful functional pipelines and enable functional programming patterns in Java.

String operations

Let us see an example where we give input and the supplier returns the value using its get method.

A simple code snippet explaining how we input a string, and how the Supplier’s get() method returns the very string.

import java.util.function.Supplier;

public class SupplierStringOperations {
  public static void main(String[] args) {
    Supplier<String> s = () -> "Hello, Developers!";

    System.out.println(s.get()); // Hello, Developers!
  }
}

Finally, another example is where we input something and do some checks, math, and string manipulations

import java.util.function.Supplier;

public class SupplierStringManipulations {
  public static void main(String[] args) {
    String fruit = "Apple";
    Supplier<Boolean> booleanSupplier = () -> fruit.length() == 5;
    Supplier<Integer> integerSupplier = () -> fruit.length() * 5;
    Supplier<String> stringSupplier = () -> fruit.toLowerCase();

    System.out.println(booleanSupplier.get());
    System.out.println(integerSupplier.get());
    System.out.println(stringSupplier.get());
  }
}

The above example prints the following on the console:

true
25
apple
Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.