It makes sense to have specializations in the Stream API, since a stream can represent bulk operations that process millions of elements, so the performance impact can be significant. But, as far as I know, even this decision was not without controversy.
For Optional , carrying no more than one element, the performance impact does not justify additional APIs (if it ever has an impact). It is not clear if OptionalInt really needed, etc.
As for convenience, I can not understand. The following works:
int j = Optional.of("1").map(Integer::parseInt).get();
your suggestion is to add another API that allows you to rewrite the above statement as
int j = Optional.of("1").mapToInt(Integer::parseInt).getAsInt();
I donβt see how it enhances convenience ...
But following the logic, with Java 9, you can write
int j = Optional.of("1").stream().mapToInt(Integer::parseInt).findFirst().getAsInt();
which enhances this "convenience" even more ...
source share