Through this example, we may simply learn how to iterate through the values contained in the HashMap object.To get a Collection of all the values in a HashMap, you need to use the values() method, and the return value is a Collection object.
Source Code
package com.beginner.examples;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
public class IterateHashMapValuesExample {
public static void main(String[] args) {
// Create HashMap object
HashMap map = new HashMap();
// Put key-value
map.put("1", "A");
map.put("2", "B");
map.put("3", "C");
// Gets the collection of values in the HashMap
Collection valueColl = map.values();
//Get iterator
Iterator iterator = valueColl.iterator();
while (iterator.hasNext()) {
String value = iterator.next();
System.out.println(value);
}
}
}
Output:
C
B
A
References
Imported packages in Java documentation: