Java Lab Programs (53) Supplier
Java Lab Programs (53) Supplier
function
Interface Supplier<T>
The Supplier Interface is a part of the java.util.function package which has been introduced since Java 8, to
implement functional programming in Java. It represents a function which does not take in any argument
but produces a value of type T. The lambda expression assigned to an object of Supplier type is used to
define its get() which eventually producesa value.
import java.util.function.Supplier;public
class Test
{
public static void main(String args[])
{
Supplier<Double> randomValue = () -> Math.random();
System.out.println(randomValue.get());
}
}
public class Test
{
static String product = "Android";
public static void main(String[] args)
{
Supplier<Boolean> boolSupplier = () -> product.length() == 10;
Supplier<Integer> intSupplier = () -> product.length() - 2; Supplier<String>
supplier = () -> product.toUpperCase();
System.out.println(boolSupplier.get());System.out.println(intSupplier.get());
System.out.println(supplier.get());
}
}
import java.util.function.Supplier;import
java.util.*;
public class SupplierDemo
{
public static void main(String[] args)
{
Supplier<Integer> supplier = SupplierDemo::getTwoDigitRandom;System.out.println(supplier.get());
}
public static Integer getTwoDigitRandom()
{
int random = new Random().nextInt(100);
if(random < 10)
return 10; return
random;
}
}
import java.util.Date;
import java.util.function.Supplier;
public class Test
{
public static void main(String args[])
{
Supplier<String> helloStrSupplier = () -> new String("Hello");
String helloStr = helloStrSupplier.get();
System.out.println("String in helloStr is->"+helloStr+"<-");