{

Java8 Primitive Supplier Examples | Functional Interface tutorials


Java8 - java.util.LongSummaryStatistics class example

Learn about the primitive supplier in java8 with examples.

What is Primitive Supplier in Java8?

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.

Java 8 introduced different Primitive Supplier interfaces in the java.util.function package.

  • IntSupplier
  • LongSupplier
  • DoubleSupplier
  • BooleanSupplier

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

The below Element class uses in all interface examples for this post.

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

java8 IntSupplier class example

IntSupplier is an interface defined in java.util.function package.

It is used as a supplier for integer results.

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

import java.util.function.IntSupplier;

public class IntSupplierExample {
    public static void main(String[] args)  throws Exception{
        IntSupplier intSupplier = ()-> 20;
        System.out.println(intSupplier.getAsInt());
        Element element = new Element(87,false);
        IntSupplier intSupplier1 = element::getNumber;
        System.out.println(intSupplier1.getAsInt());
    }
}

Output:

20  
87  

java8 DoubleSupplier class example

DoubleSupplier is a interface defined in java.util.function package. It is used as a supplier for a double result.

It has a single abstract method getAsDouble() returns the double result.

import java.util.function.DoubleSupplier;

public class DoubleSupplierExample {
    public static void main(String[] args)  throws Exception{
        DoubleSupplier doubleSupplier = ()-> 15;
        System.out.println(doubleSupplier.getAsDouble());
        Element element = new Element(14,false);
        DoubleSupplier doubleSupplier1 = element::getNumber;
        System.out.println(doubleSupplier1.getAsDouble());
    }
}

Output:

15.0  
14.0  

java8 LongSupplier class example

LongSupplier is an interface defined in java.util.function and used as a supplier for the long result.

It is a functional interface and has a single abstract method getAsLong() returns long values.

import java.util.function.LongSupplier;

public class LongSupplierExample {
    public static void main(String[] args)  throws Exception{
        LongSupplier longSupplier = ()-> 25l;
        System.out.println(longSupplier.getAsLong());
        Element element = new Element(24,false);
        LongSupplier longSupplier1 = element::getNumber;
        System.out.println(longSupplier1.getAsLong());
    }
}

Output:

25  
24  

java8 BooleanSupplier class example class

This interface is used as a supplier for Boolean values. It has only one abstract method getAsBoolean returns the Boolean value result.

import java.util.function.BooleanSupplier;

public class BooleanSupplierExample {
    public static void main(String[] args)  throws Exception{
        BooleanSupplier booleanSupplier = ()-> false;
        System.out.println(booleanSupplier.getAsBoolean());
        Element element = new Element(124,true);
        BooleanSupplier booleanSupplier1 = element::getEnabled;
        System.out.println(booleanSupplier1.getAsBoolean());
    }
}

output:

false  
true  

Conclusion

In this tutorial, Learned about primitive supplier types - IntSupplier, LongSupplier,DoubleSupplier, BooleanSupplier functional interfaces with examples

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.