I have two ways we can iterate through each entry in hashmap, can you explain which one is to be preferred over other and why ?
HashMap m = new HashMap();
m.put("A",100);
m.put("B",100);
m.put("C",100);
m.put("D",100);
One way to iterate
Set s1 = m.entrySet();
Iterator itr = s1.iterate();
while(itr.hasNext()) {
Map.Entry m1 = (Map.Entry)s1.next();
System.out.println(m1.getKey()+"..."+m1.getValue);
}
Second Way to iterate
Set<Map.Entry<String,Integer>> s1 = m.entrySet();
for(Map.Entry<String,Integer> m1:s1){
System.out.println(m1.getKey()+"..."+m1.getValue);
}