Java 8 - Best way to transform a list: map or foreach?

前端 未结 8 1862
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 07:01

I have a list myListToParse where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList.

8条回答
  •  感情败类
    2020-12-07 07:50

    I prefer the second way.

    When you use the first way, if you decide to use a parallel stream to improve performance, you'll have no control over the order in which the elements will be added to the output list by forEach.

    When you use toList, the Streams API will preserve the order even if you use a parallel stream.

提交回复
热议问题