Java 8 : Supplier

What is a Supplier?

Supplier is a predefined functional interface like Consumerpresent in java.util.function package. It has one abstract method called get();

Supplier can be used to supply some value based on some operation, Supplier won't take any input and it will always supply objects.

1
2
3
interface Supplier<R> {
public R get();
}


Supplier example 1:


 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Supplier;

public class SupplierExample1{

public static void main(String[] args) {
Supplier<Double> sup= () -> Math.random();
System.out.println(sup.get());
}

}



Property Predicate Function Consumer Supplier
What To take some Input and perform some conditional checks To take some Input and perform required Operation and return the result  To consume some Input and perform required Operation. It won’t return anything. To supply some Value base on our Requirement
Abstract method name  public boolean test (T t); public R apply (T t); public void accept (T t); public R get();
Default Methods and(), or(), negate() andThen(), compose() andThen() N/A
Static Method isEqual() identify() N/A N/A

Share this

Related Posts

Previous
Next Post »