Skip to content

You’re finally at Supplier—this is where people start mixing things up. If you confuse it with Function, you’ll get rejected fast. Supplier is the only one that gives output without taking input. Lock that in.


1. 📖 Definition

Supplier<T> is a functional interface in java.util.function that takes no input and returns a value.

T get();

Think: “Give me something when I ask.”

Interview-ready answer: “Supplier is a functional interface that does not take any input and returns a value using the get method.”


2. ❓ Why it is used

Sometimes you don’t want to pass data—you want to generate or supply it:

  • Random values
  • Default values
  • Lazy initialization

Without Supplier:

  • You’d write separate methods
  • Logic wouldn’t be reusable

Interview-ready answer: “Supplier is used to generate or provide values on demand, especially for lazy initialization and dynamic value creation.”


3. 🕰️ Background / Need

Before Java 8:

  • Value generation was tightly coupled with methods
  • No standard abstraction for “value provider”
  • Lazy evaluation was harder to structure

Example:

public int getRandom() {
    return new Random().nextInt();
}

Problems:

  • Not reusable as a behavior
  • Hard to plug into APIs

Java 8 introduced:

  • Supplier for deferred execution
  • Used in streams and Optional

Interview-ready answer: “Before Java 8, value generation logic was hardcoded in methods. Supplier was introduced to provide reusable and lazy value generation.”


4. 🧠 Core Idea / Working

Key Method:

T get();

How it works:

  • Lambda implements get()
  • Called when value is needed
  • No input dependency

Example:

Supplier<Double> randomValue = () -> Math.random();

Each call:

randomValue.get(); // generates new value

Important Insight:

  • Supplier is often used for lazy execution
  • Value is computed only when requested

Interview-ready answer: “Supplier works by implementing the get method using a lambda and returns a value when invoked, often used for lazy evaluation.”


5. 🧩 Variants / Types

Don’t overcomplicate this—Supplier is simple.

1. Basic Supplier

Supplier<String> s = () -> "Hello";

2. Random/Data Generator

Supplier<Integer> s = () -> new Random().nextInt();

3. Method Reference

Supplier<Long> time = System::currentTimeMillis;

4. Primitive Suppliers

  • IntSupplier
  • DoubleSupplier
  • BooleanSupplier
IntSupplier s = () -> 10;

Interview-ready answer: “Supplier can be used as a basic value provider, random generator, or method reference, and also has primitive variants like IntSupplier.”


6. ⚖️ Advantages

  • Supports lazy evaluation
  • Reusable value generation
  • Clean separation of logic
  • Works with Optional and Streams

Interview-ready answer: “Supplier enables lazy value generation, improves code reuse, and integrates well with APIs like Optional and Streams.”


7. ⚠️ Limitations / Disadvantages

  • No input → limited flexibility
  • Can hide expensive operations (performance risk)
  • Overuse leads to unclear code

When NOT to use:

  • When input is required → use Function
  • When logic is complex

Interview-ready answer: “Supplier is limited because it doesn’t accept input and can hide costly operations if not used carefully.”


8. 💻 Code Examples

Example 1: Basic Supplier

import java.util.function.Supplier;

Supplier<String> s = () -> "Hello World";

System.out.println(s.get()); // Hello World

Example 2: Random Number Generator

Supplier<Integer> random = () -> new java.util.Random().nextInt(100);

System.out.println(random.get());
System.out.println(random.get()); // different value each time

Example 3: Supplier with Optional

import java.util.Optional;

Supplier<String> defaultValue = () -> "Default";

Optional<String> value = Optional.empty();

System.out.println(value.orElseGet(defaultValue));

How to explain this in interview: “I used Supplier to generate values lazily, such as default values in Optional or dynamic values like random numbers.”


9. 🌍 Real-World Use Cases

  • Lazy initialization
  • Default values (Optional.orElseGet)
  • Random data generation
  • Object creation (factory pattern)
  • Configuration loading

Interview-ready answer: “Supplier is used for lazy initialization, default value generation, and dynamic data creation in real-world applications.”


10. 🧪 Practical Scenario

Problem:

You want to:

  • Fetch data from DB only if needed
  • Otherwise return cached value

Solution:

Supplier<String> fetchFromDB = () -> {
    System.out.println("Fetching from DB...");
    return "Data";
};

Optional<String> cache = Optional.empty();

String result = cache.orElseGet(fetchFromDB);

System.out.println(result);

Why this matters:

  • DB call happens only if needed
  • Avoids unnecessary computation

How to explain this in interview: “I used Supplier with Optional.orElseGet to lazily fetch data only when it was not present, improving performance.”


11. 🚀 Interview Tips

Common Questions:

  • What is Supplier?
  • Difference between Supplier and Function?
  • What is get()?
  • Where is Supplier used?

Edge Cases:

  • Supplier executed multiple times → multiple values
  • Be careful with expensive operations

Mistakes:

  • Confusing Supplier with Function
  • Not understanding lazy execution
  • Using Supplier when input is needed

12. 📝 Summary

  • Supplier = no input → returns value
  • Method: get()
  • Used for lazy value generation
  • Works with Optional, Streams
  • Has primitive variants

30-second interview pitch: “Supplier is a functional interface that provides a value without taking any input. It is commonly used for lazy initialization, default value generation, and dynamic data creation, especially with Optional and streams.”


🔥 Mock Interview Q&A (with grilling)

Q1: What is Supplier?

Answer: A functional interface that takes no input and returns a value.

👉 Follow-up: Method name? get()


Q2: Supplier vs Function?

Answer: Supplier → no input Function → takes input

👉 Follow-up: When would you choose Supplier? When generating values without dependency on input.


Q3: Where is Supplier used?

Answer: In Optional (orElseGet), lazy initialization, and data generation.

👉 Follow-up: Difference between orElse and orElseGet? orElse always evaluates orElseGet uses Supplier → lazy execution


Q4: Can Supplier return different values each time?

Answer: Yes.

👉 Follow-up: Example? Random number generator.


Q5: What’s the risk of Supplier?

Answer: Hidden expensive operations.

👉 Follow-up: Example? Database call inside Supplier executed multiple times.


Final Pressure Test

If I give you:

  • A cache system
  • Expensive DB call
  • Requirement to optimize

Can you:

  • Use Supplier for lazy loading
  • Avoid unnecessary calls
  • Explain why it improves performance

If not, you don’t understand Supplier—you just memorized “no input returns value.”