Home  >  Java 8

Java Supplier

By Arvind Rai, 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();
} 
We can see in the above code that 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!"; 
Java also provides suppliers that returns value of specific type. 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.

Instantiate Supplier using Lambda Expression

We will instantiate Supplier 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());     
  }
} 
Output
Hello World!
9 

Instantiate Supplier using Method Reference

Method reference invokes method using (::) sign. Suppose we have a class MyUtil and a static method getFavoriteBook() then we can call it using class name.
MyUtil::getFavoriteBook 
If we have non-static method then we can call this method using instance of the class. Suppose myUtil is the instance of MyUtil class and getAge() is non-static method, then we call it using instance as given below.
myUtil::getAge 
As we know that 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;
  }
} 
Output
Mahabharat
30
-682408931 

Instantiate Supplier using Default Constructor

We can instantiate Supplier using constructor with no arguments i.e. default constructor. Find the constructor reference for Book class.
Book::new 
Find the sample example to instantiate 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;
  }
} 
Output
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));      
  }
} 
Output
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());    
  }
} 
Output
true
-429015737
5525406112169000010
0.7553680537299522 

Java Supplier vs Consumer

Java Supplier 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. Find the method of Supplier functional interface.
T get() 
Find the method of Consumer functional interface.
void accept(T t) 
Now find the sample example.
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);
  }
} 
Output
Narendra Modi 

References

Interface Supplier
Java Functional Interface
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us