Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OptionalInt vs Optional<Integer>

When scrolling through the documentation for the java.util package, I was surpised to find that Optional<T> and OptionalInt have no relationship to each other. This seems very hard to belive, as it suggests that they are unrelated classes.

  1. Why don't they have a common interface, class, are sub-types, or something to reveal the relationship they have? (They're very similar classes when you look at their uses.)
  2. Also, why the need for an additional OptionalInt class? Why can't you just use Optional<Integer>? I thought it was due to the fact that int is primitive, but there is no OptionalChar so that would be an inconsistent design choice.
like image 323
Michael Avatar asked Oct 17 '15 19:10

Michael


People also ask

What is an OptionalInt?

In Java, an OptionalInt object is a container object that may or may not contain an integer value. The OptionalInt class is present in the java. util package. The getAsInt() method is used to get the integer value present in an OptionalInt object.

What is optional integer in Java?

Optional is a container object which may or may not contain a non-null value. You must import java. util package to use this class. If a value is present, isPresent() will return true and get() will return the value.

What is optional in Java Stackoverflow?

The Optional class provides APIs for returning the value of the object or a default value if the object is empty. Here, the user object was null, so user2 was returned as a default instead.


2 Answers

Java 8 introduced a whole lot dedicated to primitives. The reason is most likely that boxing primitives can create a lot of waste "boxes".

For example this

OptionalInt optionalFirst = IntStream     .range(0, 100)     .filter(i -> i % 23 > 7)     .findFirst(); 

Here, an Optional<Integer> as result would be inconsistent. Also methods like ifPresent(IntConsumer consumer) then allow to stay withing the IntStream world. Optional<Integer> would force you to convert (which you can do easily if you want)

There is no need for special support for char or short or byte because all of them can be represented as int. The missing one is boolean but there is not much you can do in a stream with them since there are only 2 values.

like image 164
zapl Avatar answered Sep 22 '22 10:09

zapl


There needs to be an OptionalInt class for Java 8's streams to be consistent. If you take a look at the Stream class, you'll see that many of the methods return Optional<T>. However, dealing with a Stream<Integer>, Stream<Long> or any other streams of primitives is exhausting, so there is an IntStream class and a LongStream class which replace the object with its unboxed value. For instance, finding the sum of the elements of a Stream<Integer> is not trivial, whereas for an IntStream, you just call IntStream#sum

In these classes, the JDK helpfully replaces Optional<T> with OptionalInt, OptionalLong, and so forth.

like image 24
Justin Avatar answered Sep 21 '22 10:09

Justin