Java Program To Iterate HashMap Using While And Advance For Loop
- Here I am going to explain on how to iterate the hashmap using while and for loop
import java.util.HashMap;
import java.unit.Iterator;
import java.util.Map;
public class HashMapIteration {
HashMap<Interger,String>map = new HashMap<Integer,String>( );
map.put(2, "Ismail");
map.put(25, "Asha");
map.put(12, "HashMap");
Systam.out.println(map.size( ) );
System.out.println("While Loop:");
Iterator itr = map.entrySet( ).iterator( );
while(itr.hasNext( ) ) {
Map.Entry me = (Map.Entry) itry.next( );
System.out.println("Key is " + me.getkey + "Value is " + me.getvalue( ) );
}
System.out.println("For Loop:");
for(Map.Entry me2: mao.entrySet( ) ) {
System.out.println("Key is: "+ me2.getkey( ) + " Value is: " + me2.ge
}
}
}
Output:
3
While Loop:
Key is 2 Value is Ismail
Key is 25 Value is Asha
Key is 12 Value is HashMap
For Loop:
Key is: 2 Value is: Ismail
Key is: 25 Value is: Asha
Key is: 12 Value is: HashMap
0 comments 10:
Post a Comment