Java8 Primitive Supplier Examples | Functional Interface tutorials

Learn about the primitive supplier in java8 with examples.

What is Primitive Supplier in Java8?

In Java 8, the java.util.function package offers various primitive supplier interfaces catering to different data types such as integers, longs, doubles, and Booleans.

This post aims to elucidate these primitive suppliers with illustrative examples.

Java 8 introduces different Primitive Supplier interfaces within the java.util.function package:

  • IntSupplier
  • LongSupplier
  • DoubleSupplier
  • BooleanSupplier All these interfaces are compatible with lambda expressions and method/constructor references.

To exemplify the usage of these interfaces, we’ll refer to the Element class provided below throughout 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 the java.util.function package. It functions as a supplier for integer results and features a single abstract method getAsInt() that returns an 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 an interface defined in the java.util.function package. It serves as a supplier for double results and features a single abstract method getAsDouble() that returns a 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 is utilized as a supplier for long results. It is a functional interface featuring a single abstract method getAsLong() that 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 employed as a supplier for Boolean values. It possesses only one abstract method, getAsBoolean, which 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

This tutorial has provided an insight into primitive supplier types - IntSupplier, LongSupplier, DoubleSupplier, and BooleanSupplier, along with functional interface examples.