Introduction
The OptionalLong
class in Java, part of the java.util
package, is a container object which may or may not contain a primitive long
value. It is designed to handle optional primitive long
values gracefully, avoiding NullPointerException
and providing a more functional approach to dealing with optional values.
Table of Contents
- What is the
OptionalLong
Class? - Common Methods
- Examples of Using the
OptionalLong
Class - Conclusion
1. What is the OptionalLong Class?
The OptionalLong
class provides a way to handle optional primitive long
values in a functional and expressive manner. Instead of using null references, OptionalLong
encapsulates the presence or absence of a long
value, making the code more readable and explicit in its intent.
2. Common Methods
empty()
: Returns an emptyOptionalLong
instance.of(long value)
: Returns anOptionalLong
with the specified value present.isPresent()
: Returnstrue
if there is a value present, otherwisefalse
.ifPresent(LongConsumer consumer)
: If a value is present, performs the given action with the value, otherwise does nothing.getAsLong()
: If a value is present, returns the value, otherwise throwsNoSuchElementException
.orElse(long other)
: Returns the value if present, otherwise returnsother
.orElseGet(LongSupplier other)
: Returns the value if present, otherwise returns the result produced by the supplying function.orElseThrow()
: Returns the contained value if present, otherwise throwsNoSuchElementException
.orElseThrow(Supplier<? extends X> exceptionSupplier)
: Returns the contained value if present, otherwise throws an exception provided by the exception supplier.
3. Examples of Using the OptionalLong Class
Example 1: Creating an OptionalLong
This example demonstrates how to create an OptionalLong
object with a value and an empty OptionalLong
.
import java.util.OptionalLong;
public class OptionalLongExample {
public static void main(String[] args) {
OptionalLong nonEmptyOptional = OptionalLong.of(100L);
OptionalLong emptyOptional = OptionalLong.empty();
System.out.println("Non-empty OptionalLong: " + nonEmptyOptional);
System.out.println("Empty OptionalLong: " + emptyOptional);
}
}
Output:
Non-empty OptionalLong: OptionalLong[100]
Empty OptionalLong: OptionalLong.empty
Example 2: Using isPresent
and ifPresent
This example shows how to check if a value is present in an OptionalLong
and perform an action if it is.
import java.util.OptionalLong;
public class OptionalLongCheckExample {
public static void main(String[] args) {
OptionalLong optional = OptionalLong.of(100L);
if (optional.isPresent()) {
System.out.println("Value is present: " + optional.getAsLong());
}
optional.ifPresent(value -> System.out.println("Value is present: " + value));
}
}
Output:
Value is present: 100
Value is present: 100
Example 3: Using orElse
and orElseGet
This example demonstrates how to provide a default value if the OptionalLong
is empty.
import java.util.OptionalLong;
public class OptionalLongDefaultExample {
public static void main(String[] args) {
OptionalLong optional = OptionalLong.empty();
long result1 = optional.orElse(0L);
long result2 = optional.orElseGet(() -> 100L);
System.out.println("Result using orElse: " + result1);
System.out.println("Result using orElseGet: " + result2);
}
}
Output:
Result using orElse: 0
Result using orElseGet: 100
Example 4: Using orElseThrow
This example shows how to throw an exception if the OptionalLong
is empty.
import java.util.OptionalLong;
public class OptionalLongExceptionExample {
public static void main(String[] args) {
OptionalLong optional = OptionalLong.empty();
try {
long result = optional.orElseThrow(() -> new IllegalArgumentException("Value is not present"));
System.out.println(result);
} catch (IllegalArgumentException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Output:
Exception: Value is not present
Example 5: Using ifPresent
with a LongConsumer
This example demonstrates how to use ifPresent
with a LongConsumer
to process the value if it is present.
import java.util.OptionalLong;
public class OptionalLongConsumerExample {
public static void main(String[] args) {
OptionalLong optional = OptionalLong.of(100L);
optional.ifPresent(value -> System.out.println("Processing value: " + value));
}
}
Output:
Processing value: 100
4. Conclusion
The OptionalLong
class in Java provides a powerful and expressive way to handle optional primitive long
values. By using OptionalLong
, developers can avoid common pitfalls associated with null references and write more robust, readable, and error-free code. The examples provided demonstrate common usage patterns and highlight the capabilities of the OptionalLong
class.
Comments
Post a Comment
Leave Comment