How to iterate through a HashMap in Java?

To iterate through a HashMap Java, use the for-each loop like so: for (var entry : map.entrySet()).

Here's how you do it:

var hashMap = new java.util.HashMap<String, String>();
hashMap.put("One", "1");
hashMap.put("Two", "2");
hashMap.put("Three", "3");

// One - 1
// Two - 2
// Three - 3
for (var entry : hashMap.entrySet()) {
  System.out.println(entry.getKey() + " - " + entry.getValue());
}