Java – optionalint vs optional
When scrolling Java I was surprised to find that the optional < T > and optionalint have nothing to do with each other This seems hard to believe because it shows that they are unrelated courses
>Why don't they have a common interface, class, subtype or thing to reveal their relationship? (when you look at their uses, they are very similar classes.) > In addition, why do you need an additional optionalint class? Why can't you use optional < integer >? I think this is because int is original, but there is no optionalchar, so it will be an inconsistent design choice
Solution
Java 8 introduces many functions for primitives The reason is probably that boxing primitives can create a lot of waste "boxes"
Like this
OptionalInt optionalFirst = IntStream .range(0,100) .filter(i -> i % 23 > 7) .findFirst();
Here, the optional < integer > results will be inconsistent Methods like ifpresent (intconsumer) also allow to leave the intstream world Optional < integer > will force you to convert (you can do it easily if you like)
There is no need to particularly support char, short or byte, because they can be expressed as int. the missing one is a Boolean value, but there is not much availability in their stream, because there are only two values