Use Iterator to loop through the HashMap class
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map errors = new HashMap();
errors.put("404", "A.");
errors.put("403", "B.");
errors.put("500", "C.");
String errorDesc = (String) errors.get("404");
System.out.println("Error 404: " + errorDesc);
Iterator iterator = errors.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println("Error " + key + " means " + errors.get(key));
}
}
}
Related examples in the same category