Home  >  Java 8

Java 8 BooleanSupplier, IntSupplier, LongSupplier and DoubleSupplier Example

By Arvind Rai, November 23, 2014
Java 8 has introduced supplier for different data type as functional interface. These are BooleanSupplier, IntSupplier, LongSupplier and DoubleSupplier classes. In this page we will learn these supplier by the example.
To create supplier we need a class which consist method that will return value to be used in our example.
Item.java
package com.concretepage.util.function;
public class Item {
    private Boolean status;
    private Integer val;
    public Item(Boolean status, Integer val){
        this.status=status;
        this.val=val;
    }
    public Boolean isStatus() {
        return status;
    }
    public Integer getVal() {
        return val;
    }
} 

BooleanSupplier

java.util.function.BooleanSupplier is a functional interface that can be used as a Supplier for Boolean values. BooleanSupplier always returns boolean value by the method getAsBoolean().
BooleanSupplierExample.java
package com.concretepage.util.function;
import java.util.function.BooleanSupplier;
public class BooleanSupplierExample {
    public static void main(String[] args) {
        Item item = new Item(true,123);
        BooleanSupplier bs = item::isStatus; 
        System.out.println("Status:"+bs.getAsBoolean());
    }
} 
Find the output.
Status:true 

IntSupplier

java.util.function.IntSupplier is a functional interface that can be used as a Supplier for integer values. IntSupplier always returns integer value by the method getAsInt().
IntSupplierExample.java
package com.concretepage.util.function;
import java.util.function.IntSupplier;
public class IntSupplierExample {
    public static void main(String[] args) {
        Item item = new Item(true,123);
        IntSupplier is = item::getVal; 
        System.out.println("Int Value:"+is.getAsInt());
    }
} 
Find the output.
Int Value:123 

LongSupplier

java.util.function.LongSupplier is a functional interface that can be used as a Supplier for long values. LongSupplier always returns long data type value by the method getAsLong().
LongSupplierExample.java
package com.concretepage.util.function;
import java.util.function.LongSupplier;
public class LongSupplierExample {
    public static void main(String[] args) {
        Item item = new Item(true,123);
        LongSupplier ls = item::getVal; 
        System.out.println("Long Value:"+ls.getAsLong());        
    }    
} 
Find the output.
Long Value:123 

DoubleSupplier

java.util.function.DoubleSupplier is a functional interface that can be used as a Supplier for double values. DoubleSupplier always returns double data type value by the method getAsDouble().
DoubleSupplierExample.java
package com.concretepage.util.function;
import java.util.function.DoubleSupplier;
public class DoubleSupplierExample {
    public static void main(String[] args) {
        Item item = new Item(true,123);
        DoubleSupplier ds = item::getVal; 
        System.out.println("Double Value:"+ds.getAsDouble());        
    }    
} 
Find the output.
Double Value:123.0 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us