Question
How can you iterate over a HashMap in Kotlin?
For example, given a Kotlin map type such as:
typealias HashMap<K, V> = java.util.HashMap<K, V>
what are the common and idiomatic ways to loop through its entries, keys, or values?
Short Answer
By the end of this page, you will understand how to iterate over a HashMap in Kotlin using idiomatic approaches such as looping through entries, keys, and values. You will also see when to use destructuring, forEach, and other common patterns used in real Kotlin codebases.
Concept
In Kotlin, a HashMap is a key-value collection. Each item in the map has:
- a key used to identify the item
- a value stored under that key
When people say they want to “iterate over a HashMap,” they usually mean one of these:
- loop through every key-value pair
- loop through only the keys
- loop through only the values
Although HashMap comes from Java, Kotlin provides cleaner and more readable ways to work with it.
A map matters in real programming because it is often used for:
- configuration settings
- user data by ID
- counting occurrences
- caching values
- request parameters
- lookup tables
In Kotlin, the most common way to iterate a map is to loop through its entries or use destructuring directly in a for loop. This is preferred because it is readable and gives direct access to both the key and the value.
Mental Model
Think of a HashMap like a locker room:
- each key is a locker number
- each value is what is stored inside that locker
Iterating over the map means walking past all the lockers and checking:
- the locker numbers only (
keys) - the contents only (
values) - or both the number and the contents (
entries)
Kotlin makes this easy by letting you open each locker and directly name the two parts:
for ((key, value) in map) {
println("$key -> $value")
}
That syntax is called destructuring: Kotlin splits each map entry into two variables for you.
Syntax and Examples
Basic syntax
Iterate over key-value pairs
val scores = hashMapOf(
"Alice" to 95,
"Bob" to 87,
"Charlie" to 91
)
for ((name, score) in scores) {
println("$name scored $score")
}
This is the most idiomatic Kotlin approach. Each map entry is unpacked into name and score.
Iterate using entries
for (entry in scores.entries) {
println("${entry.key} scored ${entry.value}")
}
Use this when you want the full entry object.
Iterate over keys only
for (name in scores.keys) {
println(name)
}
Iterate over values only
(score scores.values) {
println(score)
}
Step by Step Execution
Consider this example:
val items = hashMapOf(
"pen" to 2,
"notebook" to 5
)
for ((name, quantity) in items) {
println("$name: $quantity")
}
Here is what happens step by step:
-
A
HashMapnameditemsis created.- key
"pen"has value2 - key
"notebook"has value5
- key
-
The
forloop starts:
for ((name, quantity) in items)
-
Kotlin takes the first entry from the map.
namebecomes the keyquantitybecomes the value
Real World Use Cases
Iterating over maps is common in many real applications.
Configuration settings
val config = mapOf(
"host" to "localhost",
"port" to "8080",
"mode" to "debug"
)
for ((key, value) in config) {
println("$key = $value")
}
Used for printing or validating settings.
Counting values
val wordCounts = hashMapOf(
"kotlin" to 3,
"java" to 2,
"api" to 5
)
for ((word, count) in wordCounts) {
println("$word appears $count times")
}
Used in text processing and analytics.
API query parameters
val params = mapOf(
"page" to "1",
"sort" to "name"
)
for ((key, value) params) {
println()
}
Real Codebase Usage
In real Kotlin projects, developers usually choose the iteration style based on what they need.
Common patterns
1. Destructuring for readability
for ((key, value) in settings) {
println("$key -> $value")
}
Best when both parts are needed.
2. Guard clauses while iterating
for ((key, value) in settings) {
if (value.isBlank()) continue
println("Valid setting: $key = $value")
}
Useful for skipping invalid entries early.
3. Validation
for ((field, value) in formData) {
if (value.isBlank()) {
println("$field is required")
}
}
Common in forms, API inputs, and configuration loading.
4. Transformation with collection functions
val uppercased = scores.map { (name, score) ->
": "
}
Common Mistakes
1. Iterating over keys when you need both key and value
Broken or inefficient approach:
for (key in scores.keys) {
println("$key -> ${scores[key]}")
}
This works, but it performs a lookup for each key. A clearer approach is:
for ((key, value) in scores) {
println("$key -> $value")
}
2. Assuming HashMap keeps insertion order
Broken assumption:
val map = hashMapOf("a" to 1, "b" to 2, "c" to 3)
for ((k, v) in map) {
println(k)
}
Do not rely on the output order. If order matters, use an ordered map.
3. Forgetting that keys may not exist
val age = scores["Unknown"]
println(age)
This may print null. Kotlin maps often return nullable values when a key is missing.
Comparisons
| Approach | Example | Best when | Notes |
|---|---|---|---|
| Iterate over map directly | for ((k, v) in map) | You need both key and value | Most idiomatic Kotlin |
Iterate over entries | for (entry in map.entries) | You want the entry object | Slightly more verbose |
Iterate over keys | for (k in map.keys) | You only need keys | Avoid extra lookups if value is also needed |
Iterate over values | for (v in map.values) | You only need values | Clean and simple |
Cheat Sheet
val map = hashMapOf(
"a" to 1,
"b" to 2
)
Iterate over key-value pairs
for ((key, value) in map) {
println("$key -> $value")
}
Iterate with entries
for (entry in map.entries) {
println(entry.key)
println(entry.value)
}
Iterate over keys
for (key in map.keys) {
println(key)
}
Iterate over values
for (value in map.values) {
println(value)
}
Use forEach
map.forEach { (key, value) ->
println("$key -> $value")
}
Key rules
FAQ
How do I loop through a HashMap in Kotlin?
Use a for loop with destructuring:
for ((key, value) in map) {
println("$key -> $value")
}
What is the most idiomatic way to iterate over a map in Kotlin?
The most idiomatic approach is usually:
for ((key, value) in map)
It is concise and readable.
Can I iterate over only the keys or only the values?
Yes.
for (key in map.keys) { }
for (value in map.values) { }
Does HashMap preserve insertion order in Kotlin?
No. If you need predictable iteration order, use LinkedHashMap or linkedMapOf().
What does (key, value) mean in a Kotlin for loop?
Mini Project
Description
Build a small inventory viewer in Kotlin that stores product names and quantities in a HashMap and prints the data in different ways. This demonstrates how to iterate over entries, keys, and values in a practical program.
Goal
Create a Kotlin program that reads a map of inventory items and displays full entries, product names only, and quantities only.
Requirements
- Create a
HashMapwith at least three inventory items - Print every item as
name -> quantity - Print all product names separately
- Print all quantities separately
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.