0
0
JavaHow-ToBeginner · 3 min read

How to Use Supplier in Java: Simple Guide with Examples

In Java, Supplier is a functional interface that represents a supplier of results and has a single method get() which returns a value without taking any input. You use it when you want to generate or supply values lazily or on demand, often with lambda expressions or method references.
📐

Syntax

The Supplier interface is part of java.util.function package and has a simple syntax:

  • Supplier<T>: A generic interface where T is the type of the supplied result.
  • T get(): The single abstract method that returns a value of type T.

You implement Supplier by providing the logic inside the get() method to supply a value.

java
import java.util.function.Supplier;

Supplier<String> stringSupplier = () -> "Hello, Supplier!";

String result = stringSupplier.get();
💻

Example

This example shows how to use Supplier to provide a random number each time get() is called. It demonstrates lazy value generation without input parameters.

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

public class SupplierExample {
    public static void main(String[] args) {
        Supplier<Integer> randomSupplier = () -> new Random().nextInt(100);

        System.out.println("Random number 1: " + randomSupplier.get());
        System.out.println("Random number 2: " + randomSupplier.get());
        System.out.println("Random number 3: " + randomSupplier.get());
    }
}
Output
Random number 1: 42 Random number 2: 7 Random number 3: 89
⚠️

Common Pitfalls

Common mistakes when using Supplier include:

  • Trying to pass arguments to get() — it takes no parameters.
  • Confusing Supplier with Function which accepts input.
  • Not using lazy evaluation properly, for example, calling get() too early.

Here is a wrong and right way to use Supplier:

java
import java.util.function.Supplier;

public class SupplierPitfall {
    public static void main(String[] args) {
        // Wrong: Trying to pass argument to get() - this won't compile
        // Supplier<String> supplier = () -> "Hello";
        // String value = supplier.get("input"); // Error: get() takes no arguments

        // Right: get() takes no arguments
        Supplier<String> supplier = () -> "Hello";
        String value = supplier.get();
        System.out.println(value);
    }
}
Output
Hello
📊

Quick Reference

Supplier<T> interface quick facts:

FeatureDescription
Packagejava.util.function
MethodT get() - returns a value of type T
ParametersNone
Use caseGenerate or supply values lazily without input
Common usageWith lambda expressions or method references

Key Takeaways

Supplier provides values without input using the get() method.
Use Supplier when you want to generate or fetch data lazily or on demand.
get() method takes no arguments and returns a value of type T.
Common mistake: trying to pass arguments to get() which is not allowed.
Supplier is part of java.util.function and works well with lambdas.