Don’t worry about any performance differences, they’re going to be minimal in this case normally.
Method 2 is preferable because
-
it doesn’t require mutating a collection that exists outside the lambda expression.
-
it’s more readable because the different steps that are performed in the collection pipeline are written sequentially: first a filter operation, then a map operation, then collecting the result (for more info on the benefits of collection pipelines, see Martin Fowler’s excellent article.)
-
you can easily change the way values are collected by replacing the
Collector
that is used. In some cases you may need to write your ownCollector
, but then the benefit is that you can easily reuse that.