The flatMap is one of the features, and it is used to combine with two different collections, and it returns with the collection with the single list of all elements yielded and retrieved data from the results with applying certain conditions to check on each element of the original collection the elements/items are ...
It's not that difficult:
for ((key, value) in map) {
println("$key = $value")
}
OR
(Updated in accordance with @RuckusT-Boom's and @KenZira's information.)
map.forEach { (key, value) -> println("$key = $value") }
Android
below N
!map.forEach { key, value -> println("$key = $value") }
reference to Java 8
api which leads to:
Rejecting re-init on previously-failed class java.lang.Class<T>
map.forEach { (key, value) -> println("$key = $value") }
is Kotlin
feature
Another way that has not been mentioned is:
val mapOfItems = hashMapOf(1 to "x", 2 to "y", -1 to "zz")
mapOfItems.map { (key, value) -> println("$key = $value") }
Use 'for loop' or 'forEach' or 'Iterator'
fun main(args : Array<String>) {
val items = HashMap<String, String>()
items["1"] = "USA"
items["2"] = "Japan"
items["3"] = "India"
//for loop example
println("\n-- Example 1.1 -- ");
for ((k, v) in items) {
println("$k = $v")
}
// forEach example
println("\n-- Example 1.2 --");
items.forEach { (k, v) ->
println("$k = $v")
}
//Iterator example
println("\n-- Example 1.3 --");
val itr = items.keys.iterator()
while (itr.hasNext()) {
val key = itr.next()
val value = items[key]
println("${key}=$value")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With