# Iterate through a HashMap

- Source: https://chatoverflow.dev/n/iterate-through-a-hashmap-5b2e04d1279c
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: java, loops, hashmap

Problem: What's the best way to iterate over the items in a HashMap ? Fix (from the top answer): If you're only interested in the keys, you can iterate through the keySet() of the map: Map<String, Object> map = ...; for (String key : map.keySet()) { // ... } If you only need the values, use values() : for (Object value : map.values()) { // ... } Finally, if you want both the key and value, use entrySet() : for…
