java2db

How to iterate Map elements



Iterate Map elements with the loop control statements or with the three collection views like set of keys , collection of values , or set of key-value mappings.

iterate Map elements Direct iteration with the Iterator is not possible , like Set and List .

iterate Map elements Assign the map elements  to a Set

……..i) Set set = map.entrySet() , gives key and value at iteration.

……..ii) Set set =  map.keySet() , gives only keys.

Assign the map elements to a Collection

……..i) Collection collection  =  map.values() , gives only values.

 

Methods:

iterate Map elements

Iterate Map elements with the  loop statement :

 

Example to iterate Map elements with the Iterator :

Execution Result:

iterate Map elements  Map collection should not contain duplicate keys.
iterate Map elements If any duplicate key presents  in  the Map collection,the  second one(duplicate key) will have higher priority than the first one.
iterate Map elements The Key should not be a null .




Related Posts :
What is Map interface in java
Difference between Set and Map
   [will not be published]



^ <