In this post, we are going to see Java 8 Supplier Examples. The Supplier
interface represents a supplier of values and has a single method called get()
, which doesn’t take any arguments and returns a result.
This makes it suitable for situations where you need to generate or provide values without taking any inputs.
The main use of the Supplier
interface is to provide a way to lazily generate values or defer their computation until they are actually needed. This is particularly useful for scenarios where you want to improve performance by avoiding unnecessary computations.
In Supplier interface get()
method is defined that doesn’t accept any parameter, only returns results.
This is how the Supplier functional interface has been defined.
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Let’s see a few Java 8 Supplier examples.
Example 1: Generating Random Numbers
import java.util.Random;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<Integer> randomSupplier = () -> new Random().nextInt(100);
int randomNumber = randomSupplier.get();
System.out.println("Random Number: " + randomNumber);
}
}
Output:-
Random Number: 42
Example 2: Getting Current Date
import java.time.LocalDate;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<LocalDate> currentDateSupplier = LocalDate::now;
LocalDate currentDate = currentDateSupplier.get();
System.out.println("Current Date: " + currentDate);
}
}
Output:-
Current Date: 2023-08-21
Example 3: Generating Fibonacci Sequence
import java.util.function.Supplier;
import java.util.stream.Stream;
public class SupplierExample {
public static void main(String[] args) {
Supplier<Long> fibonacciSupplier = new Supplier<Long>() {
long a = 0, b = 1;
@Override
public Long get() {
long result = a;
long temp = a + b;
a = b;
b = temp;
return result;
}
};
Stream.generate(fibonacciSupplier)
.limit(10)
.forEach(System.out::println);
}
}
Output:-
0
1
1
2
3
5
8
13
21
34
This example demonstrates how to use a Supplier
to generate the first 10 numbers of the Fibonacci sequence using a custom implementation.
Example 4: Generating Constant Values
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<String> constantSupplier = () -> "Hello, World!";
String message = constantSupplier.get();
System.out.println(message);
}
}
Output:-
Hello, World!
Here, we create a Supplier
that always returns the same string value, “Hello, World!”.
Example 5: Reading User Input
import java.util.Scanner;
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
Supplier<String> userInputSupplier = () -> {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a message: ");
return scanner.nextLine();
};
String userInput = userInputSupplier.get();
System.out.println("You entered: " + userInput);
}
}
Output:-
Enter a message: This is an example input.
You entered: This is an example input.
This example showcases how to use a Supplier
to obtain user input from the console using the Scanner
class.
Objective questions related to Java 8 Supplier Examples.
Certainly, here are five multiple-choice objective questions related to the Java 8 Supplier
functional interface, along with their options and answers:
Question 1:
What is the primary purpose of the Java 8 Supplier
functional interface?
A) To consume values and perform an operation on them.
B) To represent a container for a single value.
C) To produce values without taking any input.
D) To transform values from one type to another.
Answer: C) To produce values without taking any input.
Question 2:
Which package contains the Supplier
interface in Java 8?
A) java.util.function
B) java.util
C) java.util.supplier
D) java.function
Answer: A) java.util.function
Question 3:
Which method is defined by the Supplier
interface in Java 8?
A) provide()
B) create()
C) get()
D) fetch()
Answer: C) get()
Question 4:
In what kind of scenario would you use the Supplier
functional interface?
A) When you want to modify an existing value.
B) When you need to consume a stream of values.
C) When you want to generate values without taking inputs.
D) When you need to filter values based on a condition.
Answer: C) When you want to generate values without taking inputs.
Question 5:
How many arguments does the get()
method of the Supplier
interface take?
A) None, it doesn’t take any arguments.
B) One, a value to be returned.
C) Two, input values to be processed.
D) Variable number, depending on the use case.
Answer: A) None, it doesn’t take any arguments.
See docs.
Other Java 8 tutorials.
- Difference between Anonymous Inner Class and Lambda Expression
- Java 8 Comparator comparing() example
- Java 8 Lambda Expressions Examples
- Write a program to find the nth Highest Salary using Java…
- Java 8 Functional Interface Examples
- Java 8 Predicate examples
That’s all about Java 8 Supplier examples.