Avatar ·

How to iterate over a HashMap in Kotlin? All ways with examples

Hi! I just started learning Kotlin after Java and encountered a simple but important task. I have a HashMap that stores key-value pairs, for example, a list of users and their IDs. I need to go through all the elements of this map and perform some actions with them: print to the console, filter, or convert to another list. In Java, I used to do this via entrySet() or keySet() in a for-each loop. But in Kotlin, as I understand, there are many more modern and concise ways. I tried just writing for (item in map), but the compiler complains. Here is an example of my code that doesn't work: ```kotlin fun main() { val users: HashMap = hashMapOf( 1 to "Anna", 2 to "Boris", 3 to "Victor" ) // Trying to pr