I already know there are only IntStream
and LongStream
. How can I make an IntStream
from a byte array?
Currently I'm planning to do like this.
static int[] bytesToInts(final byte[] bytes) {
final int[] ints = new int[bytes.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = bytes[i] & 0xFF;
}
return ints;
}
static IntStream bytesToIntStream(final byte[] bytes) {
return IntStream.of(bytesToInt(bytes));
}
Is there any easier or faster way to do this?
A variant of Radiodef's answer:
Easier to guarantee parallelization, too.
You could do something like
but it's not very pretty. (It's not so bad though: usage of
AtomicInteger
allows the stream to be run in parallel.)One line code: