Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Kotlin onEach() vs forEach() example
https://grokonez.com/kotlin/kotlin-oneach-vs-foreach-example
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:
forEach
// for Collection
public inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit
// for Map
public inline fun Iterable.forEach(action: (T) -> Unit): Unit
onEach
// for Collection
public inline fun > C.onEach(action: (T) -> Unit): C
// for Map
public inline fun > M.onEach(action: (Map.Entry) -> Unit): M
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
More at:
https://grokonez.com/kotlin/kotlin-oneach-vs-foreach-example




