In this article, we will discuss different variation of pre-defined Supplier Functional Interface available for primitive data-types like int, long, double, boolean, etc. in Java 1.8 version
Primitive Supplier Functional Interface :
- This is very similar to Supplier Functional Interface where it returns value in primitive-types like int, long, double, boolean, etc. only whereas Supplier Functional Interface return results in any data-type
- Both Supplier Functional Interface and Primitive Supplier Functional Interface doesn’t accepts any input argument
- Performance-wise primitive Supplier Functional Interface is much faster compared to Supplier<R>
- There are lot of conversion happening for auto-boxing & auto-unboxing for converting primitive-type to wrapper-type and again wrapper-type to primitive-type and so on
- To avoid unnecessary conversion between primitive-type to wrapper-type and vice-versa, primitive-type specific Supplier Functional Interface for conversion introduced in Java 1.8 version as listed below,
- IntSupplier
- LongSupplier
- DoubleSupplier
- BooleanSupplier
We will look into method signature along with example for each one of the above mentioned Primitive Supplier Functional Interface
1. IntSupplier Functional Interface
- This primitive IntSupplier Functional Interface always returns value in primitive-type int only and it is not required to declare while defining IntSupplier (or lambda expression)
- Method signature: int getAsInt();
package java.util.function;
@FunctionalInterface
public interface IntSupplier {
/**
* Gets a result
* @return a result
*/
int getAsInt();
}
1.1 Example for IntSupplier Functional Interface :
- Here, while defining lambda expression using IntSupplier we haven’t specified any data-type like Integer for return-type, still compiler doesn’t complain and executed well
package net.bench.resources.primitive.supplier.example;
import java.util.function.IntSupplier;
public class IntSupplierExample {
public static void main(String[] args) {
// 1. lambda expression to get MAX int value using IntSupplier FI
IntSupplier is1 = () -> Integer.MAX_VALUE;
System.out.println("1. Maximum integer value is = " + is1.getAsInt());
// 2. lambda expression to get random using IntSupplier FI
IntSupplier is2 = () -> (int) (Math.random() * 10);
System.out.println("\n2. Random number between 0-9 = " + is2.getAsInt());
}
}
Output:
1. Maximum integer value is = 2147483647
2. Random number between 0-9 = 4
2. LongSupplier Functional Interface :
- This primitive LongSupplier Functional Interface always returns value in primitive-type long only and it is not required to declare while defining LongSupplier (or lambda expression)
- Method signature: long getAsLong();
package java.util.function;
@FunctionalInterface
public interface LongSupplier {
/**
* Gets a result.
*
* @return a result
*/
long getAsLong();
}
2.1 Example for LongSupplier Functional Interface :
- Here, while defining lambda expression using LongSupplier we haven’t specified any data-type like Long for return-type, still compiler doesn’t complain and executed well
package net.bench.resources.primitive.supplier.example;
import java.util.function.LongSupplier;
public class LongSupplierExample {
public static void main(String[] args) {
// 1. lambda expression to get MAX long value using LongSupplier FI
LongSupplier ls1 = () -> Long.MAX_VALUE;
System.out.println("1. Maximum long value is = " + ls1.getAsLong());
// 2. lambda expression to get random using LongSupplier FI
LongSupplier ls2 = () -> (long) (Math.random() * 100000);
System.out.println("\n2. Random number upto 99999 = " + ls2.getAsLong());
}
}
Output:
1. Maximum long value is = 9223372036854775807
2. Random number upto 99999 = 1894
3. DoubleSupplier Functional Interface :
- This primitive DoubleSupplier Functional Interface always returns value in primitive-type double only and it is not required to declare while defining DoubleSupplier (or lambda expression)
- Method signature: double getAsDouble();
package java.util.function;
@FunctionalInterface
public interface DoubleSupplier {
/**
* Gets a result.
*
* @return a result
*/
double getAsDouble();
}
3.1 Example for DoubleSupplier Functional Interface :
- Here, while defining lambda expression using DoubleSupplier we haven’t specified any data-type like Double for return-type, still compiler doesn’t complain and executed well
package net.bench.resources.primitive.supplier.example;
import java.util.function.DoubleSupplier;
public class DoubleSupplierExample {
public static void main(String[] args) {
// 1. lambda expression to get MAX double value using DoubleSupplier FI
DoubleSupplier ds1 = () -> Double.MAX_VALUE;
System.out.println("1. Maximum double value is = "
+ ds1.getAsDouble());
// 2. lambda expression to get random using DoubleSupplier FI
DoubleSupplier ds2 = () -> Math.random();
System.out.println("\n2. Random double value using Math.random() = "
+ ds2.getAsDouble());
// 3. lambda expression to get pi value using DoubleSupplier FI
DoubleSupplier db3 = () -> Math.PI;
System.out.println("\n3. pi value using Math.PI() = "
+ db3.getAsDouble());
}
}
Output:
1. Maximum double value is = 1.7976931348623157E308
2. Random double value using Math.random() = 0.9430582281906369
3. pi value using Math.PI() = 3.141592653589793
4. BooleanSupplier Functional Interface :
- This primitive BooleanSupplier Functional Interface always returns value in primitive-type boolean only and it is not required to declare while defining BooleanSupplier (or lambda expression)
- Method signature: boolean getAsBoolean();
package java.util.function;
@FunctionalInterface
public interface BooleanSupplier {
/**
* Gets a result.
*
* @return a result
*/
boolean getAsBoolean();
}
4.1 Example for BooleanSupplier Functional Interface :
- Here, while defining lambda expression using BooleanSupplier we haven’t specified any data-type like Boolean for return-type, still compiler doesn’t complain and executed well
package net.bench.resources.primitive.supplier.example;
import java.util.function.BooleanSupplier;
public class BooleanSupplierExample {
public static void main(String[] args) {
// 1. lambda expression for BooleanSupplier FI
BooleanSupplier bs1 = () -> true;
System.out.println("1. value returned from 1st lambda = " + bs1.getAsBoolean());
// 2. lambda expression using BooleanSupplier FI
BooleanSupplier bs2 = () -> 1234567 > Integer.MAX_VALUE;
System.out.println("\n2. 1234567 > Integer.MAX_VALUE = " + bs2.getAsBoolean());
// 3. lambda expression using BooleanSupplier FI
BooleanSupplier bs3 = () -> 1234567 > Integer.MIN_VALUE;
System.out.println("\n3. 1234567 > Integer.MIN_VALUE = " + bs3.getAsBoolean());
}
}
Output:
1. value returned from 1st lambda = true
2. 1234567 > Integer.MAX_VALUE = false
3. 1234567 > Integer.MIN_VALUE = true
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/function/IntSupplier.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/LongSupplier.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/DoubleSupplier.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/BooleanSupplier.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Happy Coding !!
Happy Learning !!