This is the first time I am trying to use the new Stream API in Java 1.8 and I have to admit that I have some problem understanding it...
Basically, let's say I have an int array containing same values. For example:
int[] intArray = new int[]{1,2,3,4,5};
Now, I'd like to use the Stream API and store in a List the indexes of this intArray where the value is higher than 3.
This is what my code looks like:
int[] intArray = new int[]{1,2,3,4,5};
List<Integer> indexes = IntStream.range(0, intArray.length)
.filter(i -> intArray[i] > 3)
.sorted()
.collect(Collectors.toList());
But I get a compilation failure saying "The method collect(Supplier, ObjIntConsumer, BiConsumer) in the type IntStream is not applicable for the arguments (Collector >)".
I'd like indexes to contain 3 and 4.
IntStreamis a Stream of primitiveintvalues, not ofIntegerobjects. So when you create aList, those primitives values must be boxed to their correspondingInteger(because there is noIntList, i.e. a List which would hold primitive values). So you need to callboxed().