Use entrySet()
. Specification says that
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
So, you can edit set with entries instead of map and that is easier.
HashMap<Object, Object> map = new HashMap();
System.out.println("before");
map.put(1, 2);
map.put(2, null);
map.put(3, null);
for (Map.Entry<Object, Object> e : map.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}
for (Iterator<Map.Entry<Object, Object>> i = map.entrySet().iterator(); i.hasNext();) {
if (i.next().getValue() == null) {
i.remove();
}
}
System.out.println("\nafter");
for (Map.Entry<Object, Object> e : map.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}
Output:
before
1=2
2=null
3=null
after
1=2