Java 8 Supplier with examples

0
2552

1. What is Java 8 Supplier interface?

Java 8 Supplier is a functional interface whose functional method is  get(). The Supplier interface represents an operation that takes no argument and returns a result. As this is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

The following example shows how to use the get()method of the Supplier interface with Lambda expression and method reference.

2. Example with lambda expression

public class SupplierDemo {
    
    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());//false
        System.out.println(intSupplier.get());//5
        System.out.println(supplier.get());//ANDROID
        
    }
}

3. Supplier with method reference example

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;
    }
}

4. Primitive specializations of the Java 8 Supplier interface

Following are the primitive Specializations for Supplier interface in java.util.function package.

  • IntSupplier  – Represents a supplier of int-valued results. Having one method getAsInt().
  • LongSupplier – Represents a supplier of long-valued results. Having one method getAsLong().
  • DoubleSupplier – Represents a supplier of double-valued results. Having one method getAsDouble().
  • BooleanSupplier  – Represents a supplier of boolean-valued results. Having one method getAsBoolean().

4.1. Primitive specializations of Supplier interface examples

public class SupplierSpecializationDemo {

	static String product = "Android";
	static double price = 659.50;
	 
    public static void main(String[] args) {
        
        BooleanSupplier boolSupplier = () -> product.length() == 10;
        IntSupplier intSupplier = () -> product.length() - 2;
        DoubleSupplier doubleSupplier = () -> price -20;
        LongSupplier longSupplier = () -> new Date().getTime();
        Supplier<String> supplier = () -> product.toUpperCase();
        
        
        System.out.println(boolSupplier.getAsBoolean());//false
        System.out.println(intSupplier.getAsInt());//5
        System.out.println(doubleSupplier.getAsDouble());//639.5
        System.out.println(longSupplier.getAsLong());// 1581187440978 (it depends on current time)
        System.out.println(supplier.get());//ANDROID
        
    }
}

Output :

false
5
639.5
1581187440978
ANDROID

Conclusion

In this article we covered Supplier interface in java 8 and examples. In conclusion a supplier interface is represents an operation that takes no argument and returns a result, whose functional method is get() .

References

  1. Java documentation
  2. java.util.Function package tree

LEAVE A REPLY

Please enter your comment!
Please enter your name here