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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…