In this example, we will write a Java code to iterate over the values of a HashMap using the Iterator interface.
HashMap Values Iterator Example
Here is the Java example to iterate over values of a HashMap:
package com.java.tutorials.collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Iterator Examples
* @author Ramesh Fadatare
*
*/
public class IteratorExample {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
Iterator<String> iterator = map.values().iterator();
while (iterator.hasNext()) {
String value = (String) iterator.next();
System.out.println(value);
}
}
}
Output:
value1
value2
value3
value4
Collection Framework
Java
Comments
Post a Comment