The IntSupplier
functional interface in Java is part of the java.util.function
package and is designed to represent a supplier of int
-valued results. It is primarily used when we need a source of primitive int
values without generating overhead from boxing and unboxing Integer
objects.
What is IntSupplier
?
The IntSupplier
interface belongs to the functional programming tools in Java and defines a single method:
@FunctionalInterface
public interface IntSupplier {
int getAsInt();
}
- Method:
int getAsInt()
- No arguments are passed to this method.
- It returns a primitive
int
.
How to Use IntSupplier
Similar to other functional interfaces, we can use IntSupplier
in three primary ways:
1. Using Lambda Expressions
2. Using Method References
3. Using Anonymous Classes
Example 1: Using a Lambda Expression
A simple way to implement IntSupplier
is by using a lambda expression:
package org.kodejava.util.function;
import java.util.function.IntSupplier;
public class IntSupplierExample {
public static void main(String[] args) {
// Using a lambda expression to return a fixed value
IntSupplier fixedValueSupplier = () -> 42;
// Using a lambda to return a random integer (e.g., dice simulation)
IntSupplier randomValueSupplier = () -> (int) (Math.random() * 6) + 1;
// Invoke the suppliers
System.out.println("Fixed value: " + fixedValueSupplier.getAsInt());
System.out.println("Random value: " + randomValueSupplier.getAsInt());
}
}
Output:
Fixed value: 42
Random value: 3
Example 2: Using a Method Reference
We can refer to a method that matches the IntSupplier
signature (int getAsInt()
), such as a no-argument method that returns an int
:
package org.kodejava.util.function;
import java.util.function.IntSupplier;
public class IntSupplierMethodRefExample {
public static void main(String[] args) {
// Use a static method reference as an IntSupplier
IntSupplier currentTimeInSeconds = IntSupplierMethodRefExample::getCurrentTimeInSeconds;
// Invoke the supplier
System.out.println("Current time in seconds: " + currentTimeInSeconds.getAsInt());
}
// Static method compatible with IntSupplier
public static int getCurrentTimeInSeconds() {
return (int) (System.currentTimeMillis() / 1000);
}
}
Output:
Current time in seconds: 1741474123
Example 3: Using an Anonymous Class
We can also implement IntSupplier
using an anonymous class, though this approach is more verbose than lambdas or method references:
package org.kodejava.util.function;
import java.util.function.IntSupplier;
public class AnonymousClassExample {
public static void main(String[] args) {
// Use an anonymous class implementation of IntSupplier
IntSupplier counter = new IntSupplier() {
private int count = 0;
@Override
public int getAsInt() {
return ++count;
}
};
// Invoke the supplier
System.out.println(counter.getAsInt());
System.out.println(counter.getAsInt());
}
}
Output:
1
2
Using IntSupplier
in Streams
The IntSupplier
interface can be particularly useful with streams when generating values programmatically.
Example: Generate an Infinite Stream of Numbers
package org.kodejava.util.function;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class IntSupplierStreamExample {
public static void main(String[] args) {
// Supplier to generate an increasing sequence of integers
IntSupplier sequenceGenerator = new IntSupplier() {
private int current = 0;
@Override
public int getAsInt() {
return ++current;
}
};
// Use IntSupplier to create an IntStream
IntStream.generate(sequenceGenerator)
.limit(5)
.forEach(System.out::println);
}
}
Output:
1
2
3
4
5
Benefits of IntSupplier
- Avoid Autoboxing Overhead: Operates directly with primitive
int
, avoiding unnecessary boxing intoInteger
. - Lazy Evaluation: The supplier only produces values when invoked, making it ideal for on-demand generation.
- Flexibility: Can be easily combined with streams, lambdas, and other functional constructs.
Summary
IntSupplier
is a functional interface for providing primitiveint
values with itsgetAsInt()
method.- It simplifies scenarios where we need to generate or supply
int
values dynamically or repeatedly. - It integrates seamlessly with streams and functional programming concepts in Java.
- How do I use the LongToDoubleFunction functional interface in Java? - March 15, 2025
- How do I use the LongSupplier functional interface in Java? - March 14, 2025
- How do I use the LongPredicate functional interface in Java? - March 14, 2025