Java Supplier Interface

Yigit Kader
Mar 7, 2022

--

“ This interface does the opposite of the Consumer interface. So it doesn’t take anything as input but returns a value. “

Supplier has only one method which name is “ T get() ”

@FunctionalInterface
public interface Supplier<T> {
T get();
}

And here is some examples about using

///# Example 1Supplier<String> sayHello = () -> "Hello";
System.out.println(sayHello.get());
//Output : Hello
/*
* Create a random number generator with using Supplier
*/
Supplier<Integer> getRandomNumberBetween1And100 = () -> new SecureRandom().nextInt(100);
System.out.println(getRandomNumberBetween1And100.get());
//Output : Random Number Between 1 and 100

In short, there are uses in this way..

Also there are auxiliary sub-interfaces in primitive types and boolean value.

BooleanSupplier: Returns a back boolean value.
IntSupplier: Returns an int value.
DoubleSupplier: Returns a double value.
LongSupplier: Returns a long value back.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yigit Kader
Yigit Kader

Written by Yigit Kader

Software Engineer , Berlin , @devyigitk twitter

No responses yet

Write a response