Search code examples
javajava-stream

Process, map, and collect objects in one stream call


I have a list of Object and I want to first process each Object and then map them to another object and then collect them to a list. Like this:

list.stream().forEach(doSomething).map(mapFunction).collect()

But Java Stream does not support this,and I wander is there any elegant way to achieve the same result.


Solution

  • list.stream().peek(doSomething).map(mapFunction).collect()