We would like to know how to retrieve all the keys from HashMap.
import java.util.HashMap; import java.util.Iterator; import java.util.Set; /* w ww . ja v a 2 s . c om*/ public class Main { public static void main(String[] args) { HashMap<String, Integer> myMap = new HashMap<String, Integer>(); myMap.put("a",1); myMap.put("b",2); myMap.put("c",3); Set<String> stStrKeys = myMap.keySet(); Iterator<String> itrs = stStrKeys.iterator(); while (itrs.hasNext()) { String s = itrs.next(); System.out.println(s + ": " + myMap.get(s)); } } }
The code above generates the following result.