Tuesday, August 21, 2018

Java8 - OptionalDouble class Example

java.util.OptionalDouble

OptionalDouble class is introduced in java8. This is Optional container for double values that may contains empty or non empty values.if double value is present, isPresent() method returns true. getAsDouble() method returns double value.
OptionalDouble is a primitive double version of Optional class whereas Optional<Double> is an Optional class for Double Object.

OptionalDouble Example

This is an  usage of isPresent(), getAsDouble(), orElse() and Lamda expressions example.
import java.util.OptionalDouble;
public class OptionalDoubleDemo {
 public static void main(String[] args) {
  OptionalDouble optionalDouble = OptionalDouble.of(12.0);
  System.out.println(optionalDouble.isPresent()); // true
  System.out.println(optionalDouble.getAsDouble()); //12.0
  System.out.println(optionalDouble.orElse(12.0));//12.0
  optionalDouble.ifPresent(System.out::println);//12.0
  optionalDouble.ifPresent((value) -> System.out.println(value));//12.0
 }
}
The output of the above code execution is
true
12.0
12.0
12.0
12.0

How to convert Double to OptionalDouble in java8?

OptionalDouble.of() method used to create OptionalDouble with non empty values.
Syntax is 
public static OptionalDouble of(double value)
This takes double Value as input and returns OptionalDouble object.
double doubleValue = 14.3;
OptionalDouble optionDouble = OptionalDouble.of(doubleValue);
System.out.println(optionDouble.isPresent()); // returns true

How to convert OptionalDouble to Double in java8?

OptionalDouble.getAsDouble() method is used to convert to double value
OptionalDouble optionDouble = OptionalDouble.of(37.8);
  if(optionDouble.isPresent()) {
   doubleValue=optionDouble.getAsDouble();
  }
  System.out.println(doubleValue);

IntStream Average example using OptionalDouble Example

This is an example of finding the average of an array of numbers in java8.

IntStream is a sequential class used to process primitive numbers with support parallel aggregation operations.It has average() which returns OptionalDouble class. The below example uses OptionalDouble.isPresent and getAsDouble method usage a with lambda expression.
Integer[] numbersArray = {1,2,3,4,5,6,7};
Stream<Integer> streamIntegers = Arrays.stream(numbersArray);
IntStream intStreamNumber = streamIntegers.mapToInt(value -> value);
OptionalDouble numberAverage = intStreamNumber.average();
System.out.println(numberAverage.isPresent() ? 
  "Numbers Average = " + numberAverage.getAsDouble() : 
  "Empty Array");
Numbers Average = 4.0

Difference between OptionalDouble and Optional<Double> in java8?


Optional<Double>.get() method returns Double Object
OptionalDouble.getAsDouble() method returns double primitivevalue
DoubleStream and OptionalDouble objects of double value might result nullpointerexception
Optional<Double> and Stream<Double> for double value added extra wrapper to avoid null exceptions.

Methods

An optional double class has many utility methods to test values are available or not available.
Method Description
empty() Creating Empty OptionalDouble Instance
getAsDouble() Return double value, if value exists in OptionDouble, Else NoSuchElementException
ifPresent(DoubleConsumer consumer) Executes DoubleConsumer, if value exists in OptionDouble, else do nothing
isPresent() Returns true, if value exists in OptionDouble, else returns false
of(Double double) Creation of OptionalDouble with non null values, if null is passed, NullPointerException thrown and OptionDouble object will not be initlized
orElse(double other) Return default value if value is not presented in OptionalDouble
orElseGet(DoubleSupplier other) Returns value if value presented in OptionalDouble, else supplier is being called and result this will be returned
orElseThrow(Supplier exceptionSupplier) Returns value if value presented in OptionalDouble, else supplier is being called and throws an error

Related article


EmoticonEmoticon