Java8 - OptionalLong Class Example
java.util.OptionalLong
OptionalLong class is introduced in java8 along with OptionalInt and OptionalDouble. This is an optional container for long values that may or may not contain empty or nonempty values. if long value is present,isPresent() method returns true. getAsLong() method returns long value.OptionalLong is a primitive long version of Optional class whereas Optional is an Optional class for Long Object. It is defined java.util package since java8 version
Creation Empty OptionalLong Instance
empty() method creates OptionalLong instance. There is no object presented with this. isPresent() method returns false, false meaning absence of values
OptionalLong emptyOptionalLong= OptionalLong.empty();
System.out.println(emptyOptionalLong.isPresent()); // returns false
Create OptionalLong Instance with no null values
OptionalLong.of(long) method creates an OptionalLong instance with the long value presented in it.
OptionalLong valueOptionalLong = OptionalLong.of(51);
System.out.println(valueOptionalLong.isPresent()); // returns true
ifPresentOrElse() method example
ifPresentOrElse method checks, if the value exists, apply consumer on the value, else empty value.
OptionalLong optionalLongvalue = OptionalLong.of(5);
optionalLongvalue.ifPresentOrElse((value) ->{
System.out.println("data exists: " + value);
}, () -> {
System.out.println("Not exists");
});
data exist: 5
getAsLong() method example
getAsLong() method returns value if present, else throws NoSuchElementException
OptionalLong optionalLong = OptionalLong.of(20);
System.out.println(optionalLong.getAsLong());
output is
20
OptionalLong Example
This is an example of usage of Lambda expression with OptionalLong and utility methods like empty(),of(int),ifPresentOrElse()
OptionalLong emptyOptionalLong = OptionalLong.empty();
OptionalLong valueOptionalLong= OptionalLong.of(58);
valueOptionalLong.ifPresentOrElse((value) -> {
System.out.println("data exists: " + value);
}, () -> {
System.out.println("Not exists");
});
emptyOptionalLong.ifPresentOrElse((value) -> {
System.out.println("data exists: " + value);
}, () -> {
System.out.println("Not exists");
});
data exists: 58
Not exists
How to Convert OptionalLong to Long in java8
OptionalLong.getAsLong() method returns primitive long value. Follow is an example for Convert to a primitive long value
long longValue =0;
OptionalLong optionalLong = OptionalLong.of(4);
if(optionalLong.isPresent()) {
longValue=optionalLong.getAsLong();
}
System.out.println(longValue); // output 4
Output is
4
Not exists
Convert long to OptionalLong in java8
OptionalLong.of() method used to create OptionalLong with non empty long value. Syntax: public static OptionalLong of(long value) This takes long Value as input and returns OptionalLong object.
long longValue = 121l;
OptionalLong optionalLong = OptionalLong.of(longValue);
System.out.println(optionalLong.isPresent()); // returns true
System.out.println(optionalLong.getAsLong()); // returns 121
true
121
Difference between OptionalLong and Optional Long in java8?
Optional<Long>.get()
returns Long Object OptionalLong.getAsLong() method returns long primitive value IntStream and OptionalLong objects of long value have a posiblility of nullpointerexception Optional<Long>
and Stream<Long>
for long value acts as extra wrapper to avoid null exceptions.
java8 OptionalInt Methods
OptionalInt provides many utilities for handling available and not available.
Method | Description |
---|---|
empty() | Empty OptionalLong Instance will be created |
getAsLong() | Return long value, if value exists in OptionalLong, Else NoSuchElementException |
ifPresent(LongConsumer consumer) | Executes LongConsumer, if a value exists in OptionalLong, else do nothing |
isPresent() | Returns true, if a value exists in OptionalLong, else returns false |
of(long value) | Creation of OptionalLong with nonnull values, if null is passed, NullPointerException has thrown and OptionalLong object will not be initialized. |
orElse(long other) | Return default value if the value is not presented in OptionalLong |
orElseGet(LongSupplier other) | Returns value if value presented in OptionalLong, else supplier is being called and result in this will be returned. |
orElseThrow(Supplier exceptionSupplier) | Returns value if value presented in OptionalLong, else supplier is being called and throws an error. |