I was confused about the difference between map() and forEach() method in java8 stream. For instance,
List strings =
The difference is this:
forEach() is to "work" on each element of the underlying collection (by having a side effect) whereas map() is about applying a method on each object and putting the result of that into a new streamThat is also the reason why your stream().map() doesn't result in something - because you throw away the new stream created by the map() call!
In that sense, the signatures of the two methods tell you that:
void forEach(BiConsumer super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed
versus
Stream map(Function super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.
And for the record: only map() is a stream method - forEach() exists for both, streams and Collections/Iterables.