Java8 Primitive Supplier Examples | Functional Interface tutorials


Primitive Supplier Examples

java.lang.function package provides various primitive supplier interfaces. java8 has functional interfaces for different data types like integer, long, double and boolean. This post explains various primitive supplier and usage examples.

All these interfaces can be assigned with lambda expressions and method/constructor reference.

The Element class is used in all interface examples.

package org.cloudhadoop.functions;  
public class Element {  
  
 private Boolean enabled;  
 private Integer number;  
  
 public Element(Integer number, Boolean enabled) {  
  this.enabled = enabled;  
  this.number = number;  
 }  
  
 public Boolean getEnabled() {  
  return enabled;  
 }  
  
 public void setEnabled(Boolean enabled) {  
  this.enabled = enabled;  
 }  
  
 public Integer getNumber() {  
  return number;  
 }  
  
 public void setNumber(Integer number) {  
  this.number = number;  
 }  
}  

IntSupplier class

java.util.function.IntSupplier interfaces is used as a supplier for integer result.

It has single abstract method getAsInt() returns integer value.

IntSupplier intSupplier = ()-> 20;  
System.out.println(intSupplier.getAsInt());  
Element element = new Element(87,false);  
IntSupplier intSupplier1 = element::getNumber;   
System.out.println(intSupplier1.getAsInt());  

Output is

20  
87  

DoubleSupplier class

java.util.function.DoubleSupplier interfaces are used as a supplier for a double result. It has single abstract method getAsDouble() returns the double result.

DoubleSupplier doubleSupplier = ()-> 15;  
System.out.println(doubleSupplier.getAsDouble());  
Element element = new Element(14,false);  
DoubleSupplier doubleSupplier1 = element::getNumber;   
System.out.println(doubleSupplier1.getAsDouble());  

Output is

15.0  
14.0  

LongSupplier class

This interface is defined in java.util.function and used as a supplier for the long result. It has single abstract method getAsLong() returns long values.

LongSupplier longSupplier = ()-> 25l;  
System.out.println(longSupplier.getAsLong());  
Element element = new Element(24,false);  
LongSupplier longSupplier1 = element::getNumber;   
System.out.println(longSupplier1.getAsLong());  

Output is

25  
24  

BooleanSupplier class

This interface is used as a supplier for boolean values. It has only one abstract method getAsBoolean return boolean value result.

 BooleanSupplier booleanSupplier = ()-> false;  
System.out.println(booleanSupplier.getAsBoolean());  
Element element = new Element(124,true);  
BooleanSupplier booleanSupplier1 = element::getEnabled;   
System.out.println(booleanSupplier1.getAsBoolean());

output is

false  
true  

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.