0% found this document useful (0 votes)
10 views1 page

Java Lab Programs (53) Supplier

The Supplier interface represents a function that produces a value without accepting any arguments. It is part of the java.util.function package introduced in Java 8 to support functional programming. A Supplier can be used to define lambda expressions that return values when their get() method is called, allowing values to be lazily obtained. Examples show Suppliers being used to return random numbers, string lengths, uppercase strings, random integers between 0-99, and the current date.

Uploaded by

vinol30054
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Java Lab Programs (53) Supplier

The Supplier interface represents a function that produces a value without accepting any arguments. It is part of the java.util.function package introduced in Java 8 to support functional programming. A Supplier can be used to define lambda expressions that return values when their get() method is called, allowing values to be lazily obtained. Examples show Suppliers being used to return random numbers, string lengths, uppercase strings, random integers between 0-99, and the current date.

Uploaded by

vinol30054
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

java.util.

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+"<-");

Supplier<Date> dateSupplier= Test::getSystemDate;


Date systemDate = dateSupplier.get();
System.out.println("systemDate->" + systemDate);
}
public static Date getSystemDate()
{
return new Date();
}
}

You might also like