.map questions

Hi. I found this little code on the internet:


I’m just curious about line 4. with it.value program prints out numbers 20 and 21. How did it know to print out just numbers with the word value? Is the second element of the map always a value? By the same logic, how to print out only the names?
println(peopleToAge.map { it.string}) Something like it?

Also, if I have questions of this sort, should I put it to the Language Design category or some other?
Thank You

A map consists of map entries and each entry has a key and a value. So, if you iterate over a map, what the map function does, you are effectively iterating over a collection of map entries. If you replace it.value with it.key you will see the keys. But I think it would be more efficient and readable, to use the keys directly:

println(myMap.keys)
2 Likes

And to answer your second question: no, this is not about language design. It is just a general question of someone learning Kotlin.

Language design is about the language itself, maybe if it should be possible to write an anonymous function in curly braces outside of the actual parameter list. Or maybe if Kotlin should have deep immutability enforced by the compiler and what keyword should be used for it.

2 Likes

Thank You. :slight_smile: