Java8 - OptionalInt class Example
java.util.OptionalInt
OptionalInt is a container class that contains null or not null int values. OptionalInt is a primitive int version of Optional class whereas Optional<Integer>
is an Optional class for Integer Object. It is defined java.util package and aviable since java8 version. isPresent() method returns true. getAsInt() method returns integer value.
Creation Empty OptionalInt Instance
empty() method creates OptionalInt instance. No object presented in it. isPresent() method returns false, false meaning absense of values
OptionalInt emptyOptionalInt = OptionalInt.empty();
Create non empty OptionalInt Instance
OptionalInt.of(int) method creates OptionalInt instance with int value presented in it.
OptionalInt valueOptionalInt = OptionalInt.of(5);
ifPresentOrElse() method example
ifPresentOrElse method checks, if the value exists, apply consumer on the value, else empty value
OptionalLong optionalLongvalue = OptionalLong.of(15);
optionalIntvalue.ifPresentOrElse((value) -> {
System.out.println("data exist: " + value);
}, () -> {
System.out.println("Not exists");
});
Output is
data exist: 5
OptionalInt example
This is an example of usage of Lambda expression with OptionalInt and utility methods like empty(),of(int),ifPresentOrElse()
OptionalInt emptyOptionalInt = OptionalInt.empty();
OptionalInt optionalIntvalue = OptionalInt.of(5);
optionalIntvalue.ifPresentOrElse((value) -> {
System.out.println("data exist: " + value);
}, () -> {
System.out.println("Not exists");
});
emptyOptionalInt.ifPresentOrElse((value) -> {
System.out.println("data exist: " + value);
}, () -> {
System.out.println("Not exists");
});
data exist: 5
Not exists
Convert OptionalInt to Int in java8
OptionInt.getAsInt() method returns primitive int value
int intValue =0;
OptionalInt optionalInt = OptionalInt.of(3);
if(optionalInt.isPresent()) {
intValue=optionalInt.getAsInt();
}
System.out.println(intValue); // output 3
Convert Int to OptionalInt in java8
OptionalInt.of() method used to create OptionalInt with non empty int value. Syntax public static OptionalInt of(int value) This takes int Value as input and returns the OptionalInt object.
int intValue = 17;
OptionalInt optionInt = OptionalInt.of(intValue);
System.out.println(optionInt.isPresent()); // returns true
Difference between OptionalInt and Optional in java8?
Optional<Integer>.get()
returns Integer Object OptionalInt.getAsInt() method returns int primitivevalue IntStream and OptionalInt objects of int value might result nullpointerexception Optional<Integer>
and Stream<Integer>
for int value added extra wrapper to avoid null exceptions.
Methods
OptionalInt provides many utilities for handling available and not available.
Method
Description
empty()
Empty OptionalInt Instance Creation
getAsInt()
Return int value, if value exists in OptionalInt, Else NoSuchElementException
ifPresent(IntegerConsumer consumer)
Executes IntegerConsumer, if the value exists in OptionalInt, else do nothing
isPresent()
Returns true, if value exists in OptionalInt, else returns false
of(Integer integer)
Creation of OptionalInt with nonnull values, if null is passed, NullPointerException threw and OptionalInt object will not be initialized
orElse(int other)
Return default value if the value is not presented in OptionalInt
orElseGet(IntegerSupplier other)
Returns value if value presented in OptionalInt, else supplier is being called and result from this will be returned
orElseThrow(Supplier exceptionSupplier)
Returns value if value presented in OptionalInt, else supplier is being called and throws an error