The LongSupplier is a functional interface in Java that is part of the java.util.function package. It represents a function that supplies a long-valued result. This interface is particularly useful when we need to generate or provide long values without requiring input arguments.
Since LongSupplier is a functional interface, it has a single abstract method:
long getAsLong(): This method is used to return alongvalue.
Here is how we can use the LongSupplier interface in Java:
Example 1: Basic Usage with Lambda Expression
package org.kodejava.util.function;
import java.util.function.LongSupplier;
public class LongSupplierExample {
public static void main(String[] args) {
// Using a LongSupplier to provide the current system time in milliseconds
LongSupplier currentTimeSupplier = () -> System.currentTimeMillis();
// Getting a long value
long currentTime = currentTimeSupplier.getAsLong();
System.out.println("Current Time in Milliseconds: " + currentTime);
}
}
Example 2: Using Method References
We can also use method references instead of a lambda if it matches the expected signature of the LongSupplier.
package org.kodejava.util.function;
import java.util.function.LongSupplier;
public class LongSupplierExample2 {
public static void main(String[] args) {
// Using method reference
LongSupplier nanoTimeSupplier = System::nanoTime;
// Getting a long value
long nanoTime = nanoTimeSupplier.getAsLong();
System.out.println("Current Time in Nanoseconds: " + nanoTime);
}
}
Example 3: Custom Implementation
We can create our own implementation of LongSupplier.
package org.kodejava.util.function;
import java.util.function.LongSupplier;
public class LongSupplierExample3 {
public static void main(String[] args) {
// Custom implementation
LongSupplier randomLongSupplier = new LongSupplier() {
@Override
public long getAsLong() {
// Generate a random long value
return (long) (Math.random() * 1000);
}
};
// Getting a long value
long randomValue = randomLongSupplier.getAsLong();
System.out.println("Random Long Value: " + randomValue);
}
}
Example 4: Using with Streams
LongSupplier can also be useful when used with streams for generating a sequence of long values.
package org.kodejava.util.function;
import java.util.function.LongSupplier;
import java.util.stream.LongStream;
public class LongSupplierExample4 {
public static void main(String[] args) {
// LongSupplier to generate an infinite series of long values
LongSupplier supplier = new LongSupplier() {
private long start = 1;
@Override
public long getAsLong() {
return start++;
}
};
// Using the LongSupplier with LongStream.generate
LongStream.generate(supplier)
.limit(10) // Limit to 10 values
.forEach(System.out::println); // Print each value
}
}
Key Points:
LongSupplieris a no-argument functional interface that always returns alongvalue.- It is lightweight and simple to use with lambdas, method references, or custom implementations.
- Useful in scenarios where we need to continuously or repeatedly generate
longvalues, such as timestamps, counters, or random numbers.
