Timeline for Java Stream API - Best way to transform a list: map or forEach?
Current License: CC BY-SA 3.0
3 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 29, 2021 at 17:38 | comment | added | rogerdpack |
OK. According to docs.oracle.com/javase/tutorial/collections/streams/… the collect method is designed to perform the most common stream operations that have side effects in a parallel-safe manner. Operations like forEach and peek ... if you use one of these operations with a parallel stream, then the Java runtime may invoke the lambda expression that you specified as its parameter concurrently from multiple threads. So it carefully has some things atomic and some not. Weird. Assuming that is the case, option 1, if made parallel, might have contention issues...
|
|
Feb 4, 2015 at 11:08 | comment | added | herman |
I'm not sure this is correct advice: he could use forEachOrdered instead of forEach if he wanted to use a parallel stream but still preserve the order. But as the documentation for forEach states, preserving the encounter order sacrifices the benefit of parallelism. I suspect that is also the case with toList then.
|
|
Feb 4, 2015 at 10:34 | history | answered | Eran | CC BY-SA 3.0 |