Java Supplier Example
February 21, 2019
Java Supplier
is a functional interface that represents a supplier of results. The functional method of Supplier
is get()
. A Supplier
can be instantiated using lambda expression or method reference or default constructor. The Supplier
has been introduced in Java 8 and belongs to java.util.function
package. The source code of Supplier
functional interface is as following.
@FunctionalInterface public interface Supplier<T> { T get(); }
Supplier
has get()
method that returns generic type value. The method get()
will not accept any argument and only return the value of generic type. We can instantiate Supplier
as following.
Supplier<String> s = () -> "Hello World!";
BooleanSupplier
returns Boolean data type, IntSupplier
returns integer data type, LongSupplier
returns long data type and DoubleSupplier
returns double data type values. We can also create custom functional interface according to our requirements.
Contents
Instantiate Supplier using Lambda Expression
We will instantiateSupplier
here using lambda expression. As we know that its method get()
only returns value and does not accept any argument, so our lambda expression will have empty argument part.
SupplierWithLambda.java
package com.concretepage; import java.util.Random; import java.util.function.Supplier; public class SupplierWithLambda { public static void main(String[] args) { Supplier<String> s1 = () -> "Hello World!"; System.out.println(s1.get()); Random random = new Random(); Supplier<Integer> s2 = () -> random.nextInt(10); System.out.println(s2.get()); } }
Hello World! 9
Instantiate Supplier using Method Reference
Method reference invokes method using (::) sign. Suppose we have a classMyUtil
and a static method getFavoriteBook()
then we can call it using class name.
MyUtil::getFavoriteBook
myUtil
is the instance of MyUtil
class and getAge()
is non-static method, then we call it using instance as given below.
myUtil::getAge
get()
method of Supplier
functional interface has no arguments, so our methods getFavoriteBook()
and getAge()
should not accept any argument. Find the example.
SupplierWithMethodReference.java
package com.concretepage; import java.util.Random; import java.util.function.Supplier; public class SupplierWithMethodReference { public static void main(String[] args) { Supplier<String> s1 = MyUtil::getFavoriteBook; System.out.println(s1.get()); MyUtil myUtil = new MyUtil(); Supplier<Integer> s2 = myUtil::getAge; System.out.println(s2.get()); Random random = new Random(); Supplier<Integer> s3 = random::nextInt; System.out.println(s3.get()); } } class MyUtil { private Integer age = 30; public static String getFavoriteBook(){ return "Mahabharat"; } public Integer getAge(){ return age; } }
Mahabharat 30 -682408931
Instantiate Supplier using Default Constructor
We can instantiateSupplier
using constructor with no arguments i.e. default constructor. Find the constructor reference for Book
class.
Book::new
Supplier
using default constructor.
SupplierWithConstructorRef.java
package com.concretepage; import java.util.Random; import java.util.function.Supplier; public class SupplierWithConstructorRef { public static void main(String[] args) { Supplier<Random> s1 = Random::new; Random random = s1.get(); System.out.println(random.nextInt(10)); Supplier<Book> s2 = Book::new; Book book = s2.get(); System.out.println(book.getBookName()); } } class Book { private String bookName = "Mahabharat"; public String getBookName(){ return bookName; } }
9 Mahabharat
Custom Supplier Functional Interface
We can create a custom supplier functional interface using@FunctionalInterface
annotation. Our supplier we take no argument but return a value of generic type.
CustomSupplierDemo.java
package com.concretepage; import java.util.Random; @FunctionalInterface interface MySupplier<T> { T fetch(); } public class CustomSupplierDemo { public static void main(String[] args) { //Using Lambda Expression MySupplier<String> s1 = () -> "Hello World!"; System.out.println(s1.fetch()); //Using Method Reference Random random = new Random(); MySupplier<Integer> s2 = random::nextInt; System.out.println(s2.fetch()); //Using Constructor MySupplier<Random> s3 = Random::new; Random rdm = s3.fetch(); System.out.println(rdm.nextInt(10)); } }
Hello World! 521143516 6
BooleanSupplier, IntSupplier, LongSupplier, DoubleSupplier
Java provides following functional interfaces that are used for specific data type supplier.BooleanSupplier: Supplier to return Boolean value. Its method is
getAsBoolean()
.
IntSupplier: Supplier to return integer data type value. Its method is
getAsInt()
.
LongSupplier: Supplier to return long data type value. Its method is
getAsLong()
.
DoubleSupplier: Supplier to return double data type value. Its method is
getAsDouble()
.
Find the sample example.
SpecificDataTypeSupplier.java
package com.concretepage; import java.util.Random; import java.util.function.BooleanSupplier; import java.util.function.DoubleSupplier; import java.util.function.IntSupplier; import java.util.function.LongSupplier; public class SpecificDataTypeSupplier { public static void main(String[] args) { int age = 30; BooleanSupplier bs = () -> age > 20; System.out.println(bs.getAsBoolean()); Random random = new Random(); IntSupplier is = random::nextInt; System.out.println(is.getAsInt()); LongSupplier ls = random::nextLong; System.out.println(ls.getAsLong()); DoubleSupplier ds = random::nextDouble; System.out.println(ds.getAsDouble()); } }
true -429015737 5525406112169000010 0.7553680537299522
Java Supplier vs Consumer
JavaSupplier
and Consumer
both are functional interfaces. Supplier
represents a supplier of results that returns an object and accepts no arguments whereas Consumer
represents an operation that accepts a single input argument and returns no result.
Supplier
functional interface.
T get()
Consumer
functional interface.
void accept(T t)
SupplierConsumerDemo.java
package com.concretepage; import java.util.function.Consumer; import java.util.function.Supplier; public class SupplierConsumerDemo { public static void main(String[] args) { Supplier<String> s = Country::getPMName; Consumer<String> c = Country::printMessage; c.accept(s.get()); } } class Country { public static String getPMName() { return "Narendra Modi"; } public static void printMessage(String msg) { System.out.println(msg); } }
Narendra Modi
References
Interface SupplierJava Functional Interface