I have a simple stream like following:
List<Long> r = l.stream()
.filter(a->a.getB() % 2 == 0)
.map(A::getB)
.collect(Collectors.toList());
But Intellij suggests me:
'filter()' and 'map()' can be swapped Inspection info: Reports stream API call chains which can be simplified. It allows to avoid creating redundant temporary objects when traversing a collection. e.g.
- collection.stream().forEach() → collection.forEach()
- collection.stream().collect(toList/toSet/toCollection()) → new CollectionType<>(collection)
The example given by Intellij is easy to understand, but I don't understand why it suggests me to map().filter()
.
I view the source of ReferencePipeline
but find no clue: map().filter()
or filter().map()
makes no difference when comes to the temporary object related to stream implementation (filter().map()
will have less auto-boxing if A.b
is a primitive which makes me more confused).
So, am I missing some point of stream implementation or this is a false alarm of Intellij?