Java8 - OptionalInt class Example


java.util.OptionalInt

OptionalInt is a container class which contains null or not null int values.
OptionalInt is a primitive int version of Optional class whereas Optional 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 value exists, apply consumer on the value, else empty value

OptionalLong optionalLongvalue = OptionalLong.of(15);   
  optionalIntvalue.ifPresentOrElse((value) -> {  
  System.out.println("data exists: " + value);  
  }, () -> {  
  System.out.println("Not exists");   
  });

Output is

data exists: 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 exists: " + value);  
}, () -> {  
System.out.println("Not exists");   
});  
emptyOptionalInt.ifPresentOrElse((value) -> {  
System.out.println("data exists: " + value);  
}, () -> {  
System.out.println("Not exists");   
});  

data exists: 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 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.get() returns Integer Object
OptionalInt.getAsInt() method returns int primitivevalue
IntStream and OptionalInt objects of int value might result nullpointerexception
Optional and Stream 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 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 non null values, if null is passed, NullPointerException thrown and OptionalInt object will not be initlized

orElse(int other)

Return default value if value is not presented in OptionalInt

orElseGet(IntegerSupplier other)

Returns value if value presented in OptionalInt, else supplier is being called and result this will be returned

orElseThrow(Supplier exceptionSupplier)

Returns value if value presented in OptionalInt, else supplier is being called and throws an error

Similar Posts