Kotlin onEach() vs forEach() example


In the tutorial, JavaSampleApproach will show the difference between Kotlin onEach() vs forEach().

I. Kotlin onEach vs forEach

Kotlin provides 2 methods to perform the given [action] on each element: onEach and forEach. Method signature:


What is the main difference between forEach() vs onEach()?
-> forEach() method just performs the given [action] on each element. While onEach() method performs the given [action] on each element and returns the collection itself afterwards.

1. with Kotlin Collection

2. with Kotlin Map

II. Full Sourcecode

By grokonez | January 14, 2018.

Last updated on September 6, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *