Java 8 forEach use cases -
let's have collection strings , want return first 2 characters of each string (or other manipulation...).
in java 8 case can use either map
or foreach
methods on stream() collection (maybe else not important right now).
personally use map
because associate foreach
mutating collection , want avoid this. created small test regarding performance not see improvements when using foreach
(i understand small tests cannot give reliable results still).
so use-cases 1 should choose foreach
?
map
better choice this, because you're not trying do strings yet, map them different strings.
foreach
designed "final operation." such, doesn't return anything, , mutating some state -- though not of original collection. instance, might use write elements file, having used other constructs (including map
) elements.
Comments
Post a Comment