Java Supplier Interface
“ 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.