java iterate map values code example

Example 1: java iterate through hashmap

for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
	System.out.println(entry.getKey() + " = " + entry.getValue());
}

Example 2: java for map

Map<String, String> map = new HashMap<>();

for(Entry<String, String> entry:map.entrySet()) {
  System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
}

Example 3: java iterate through map

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}

Example 4: map interation in java

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

// Iterating over keys only
for (Integer key : map.keySet()) {
    System.out.println("Key = " + key);
}

// Iterating over values only
for (Integer value : map.values()) {
    System.out.println("Value = " + value);
}

Tags:

Java Example