Java Supplier
last modified July 8, 2023
Java Supplier tutorial shows how to work with the Supplier functional interface in Java.
Supplier
Java Supplier is a functional interface which represents an operation
that returns a result. Supplier
does not take any arguments.
@FunctionalInterface public interface Supplier<T> { T get(); }
T
is the type of results supplied by the supplier.
Java Supplier example
The following example creates a simple supplier.
package com.zetcode; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; public class SupplierEx { public static void main(String[] args) { var words = List.of("falcon", "cup", "fun", "cloud"); var res = upperWords(words); System.out.println(res); } public static List<String> upperWords(List<String> words) { var uppered = new ArrayList<String>(); for (var word : words) { Supplier<String> upperSupplier = word::toUpperCase; uppered.add(upperSupplier.get()); } return uppered; } }
The program turns words into uppercase using upperSupplier
.
Supplier<String> upperSupplier = word::toUpperCase;
The Supplier
returns a string. It references the
toUpperCase
method of the String
type.
[FALCON, CUP, FUN, CLOUD]
Java IntSupplier
IntSupplier
represents a supplier of int-valued results. This is
the int-producing primitive specialization of Supplier
.
import java.util.stream.IntStream; public class SupplierEx2 { public static void main(String[] args) { IntSupplier randIntSupp = () -> new Random().nextInt(40); System.out.println(randIntSupp.getAsInt()); System.out.println(randIntSupp.getAsInt()); System.out.println("--------------------"); IntStream.generate(randIntSupp).limit(6).forEach(System.out::println); } }
The program creates a supplier of random integers.
IntSupplier randIntSupp = () -> new Random().nextInt(40);
The IntSupplier
returns a random integer in range [0,40).
System.out.println(randIntSupp.getAsInt());
We get a random integer with getAsInt
.
IntStream.generate(randIntSupp).limit(6).forEach(System.out::println);
A stream of six random integers is generated.
4 35 -------------------- 31 4 10 30 15 8
Java CompletableFuture.supplyAsync
The Supplier
interface is used in various Java APIs. For instance,
the CompletableFuture.supplyAsync
returns a
CompletableFuture
that is asynchronously completed.
package com.zetcode; import java.util.Random; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; public class SupplierEx3 { public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } return new Random().nextInt(40); }); System.out.println(future.get()); } }
The program returns a random integer in range [0,40) in an async operation.
In this article we have worked with Java Supplier interface.
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } return new Random().nextInt(40); });
CompletableFuture
takes a Supplier
as a parameter.
In this article we have worked with the Supplier
Java interface.
Author
List all Java tutorials.