1

Simply, I'm trying to iterate over an array using a stream to find out if every element is the same type of object. I have figured out two ways of doing this (foo is a byte array byte[]).

Arrays.asList(foo).parallelStream().allMatch(aByte -> aByte == Byte.MIN_VALUE);

and

Stream.of(foo).parallel().allMatch(aByte -> aByte == Byte.MIN_VALUE);

However, I keep getting the error "Incompatible operand types byte[] and byte". So that means that aByte is of type byte[]. Why is that? Why is it not just a normal byte? And how may I correct this problem?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
danthonywalker
  • 368
  • 3
  • 14

1 Answers1

4

If foo is of type byte[], then Arrays.asList(foo) will return a list containing a single element: foo.

It would return a list containing all the elements of foo is foo was an array of Objects. But it's not, because byte is a primitive type, not an Object. And a byte[] is thus not an Object[].

What you should use in fact is an IntStream. See What is the best way to convert a byte array to an IntStream? for how to convert a byte array to an IntStream.

Note that using a parallel stream for such a simple task is not a good idea. Unless the byte array is awfully huge and contains only Byte.MIN_VALUE, the overhead of a parallel stream over a sequential one will make things slower.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255