When We have for lop then why we have use iterator following code are some working as Result give by iterator
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, String> hasmap = new HashMap<String, String>();
hasmap.put("1", "Java");
hasmap.put("2", ".Net");
hasmap.put("3", "C++");
hasmap.put("4", "Pearl");
hasmap.put("5", "PHP");
for (Entry<String, String> entry : hasmap.entrySet()) {
System.out.println("For Loop Get Key"+entry.getKey());
System.out.println("For Loop Get Value"+entry.getValue());
}
Set set = hasmap.entrySet();
Iterator itr = set.iterator();
while(itr.hasNext()){
Map.Entry map = (Entry) itr.next();
System.out.println(map.getKey());
System.out.println(map.getValue());
}
}
}