I have HashMap<String,String>(called p2p), and I want to loop over its elements. I found the following simple way to do this:
for (String key : p2p.keySet()) {
value = p2p.get(key);
}
However, later I found out what people use iterator(). For instance:
Iterator it = p2p.keySet().iterator();
while(it.hasNext()) {
key = it.next();
value = p2p.get(key);
}
For me, the first way looks simpler. So my question is why do people use the second way? Does it have some objective advantages or is it just a matter of taste and subjectivity of simplicity?
source
share