Reputation: 13915
Given int[] A. Trying to make use all elements in array is greater or equals to -1000 and not greater than 1000 (incl.). How to do it properly?
My try:
boolean validIntegers = (Arrays.asList(A)).stream().allMatch(i -> (i >= -1000 && i <= 1000) );
Error:
Upvotes: 3
Views: 1736
Reputation: 124275
Arrays.asList
accepts T...
but T
as generic type can represent only Objects like int[]
, not primitive int
. So T...
represents {int[]}
array, which contains internally array object, not array elements. So your stream contains array and you can't use any comparison operators on array.
To solve this problem and get stream of elements stored in array of int
s you can use
IntStream.of(int...)
, Arrays.stream(yourArray)
which supports double[]
int[]
long[]
and T[]
.So your code could be
boolean validIntegers = IntStream.of(A).allMatch(i -> (i >= -1000 && i <= 1000) );
Upvotes: 6