Java and Spring Level 1 This level · 25 of 105
Easy java streams

filter versus map

The 30 second answer

filter keeps or drops elements with a predicate; the type stays the same and the count can only shrink. map transforms every element with a function; the count stays the same and the type may change. They solve different jobs and are often chained together.

Could you say this in an interview?

filter(Predicate) keeps or drops elements — type unchanged, count ≤ input. map(Function) transforms each element — count unchanged, type may change.

Sources (1)
  1. Stream.filter / mapdocs.oracle.com
Previous
Next