we cannot iterate a Map directly using iterators, because Map are not Collection.
There are different methods to iterate through MAP elements:
Method 1: Using Iterator and keySet().
package seleniumrepo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
Map<String,String> mp=new HashMap<String,String>();
mp.put("1", "avinash");
mp.put("2", "venu");
mp.put("3", "dharma");
mp.put("4", "hari");
Iterator<String> it=mp.keySet().iterator();
while(it.hasNext()){
String temp=it.next();
System.out.println(temp + " " +mp.get(temp));
}
}
}
Method 2: Using foreach() method. It is defined as default method in the Map interface.
package seleniumrepo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("1", "avinash");
map.put("2", "venugopal");
map.put("3", "dharma");
map.put("4", "hari");
map.forEach((k,v)->System.out.println("Key: "+k+" Value: "+v));
}
Method 3: Using entrySet() and Map.Entry(K, V).
package javaprograms;
import java.util.HashMap;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("1", "avinash");
map.put("70", "venugopal");
map.put("30", "dharma");
map.put("4", "hari");
System.out.println(map.values());
for(Map.Entry<String, String> entry: map.entrySet()) {
System.out.println("Key: "+entry.getKey()+"\t"+ "Value: "+entry.getValue());
}
}
}
Output:
[avinash, hari, venugopal, dharma]
Key: 1 Value: avinash
Key: 4 Value: hari
Key: 70 Value: venugopal
Key: 30 Value: dharma
There are different methods to iterate through MAP elements:
Method 1: Using Iterator and keySet().
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuPbt_2z65whinJHCzEAsXAd0c88fwRQW0wLYiFcOxtbgnhyphenhyphen_4S4-On1OA0DC9vRj9iZqb-DdhIVwyhXuuSYnPV368O84eVyNDnAXSZi72j4jhYaVFJ3T8v_ZYbfRmv7SuPOjiEV4aEFU/s320/mapelementsiteration.png)
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
Map<String,String> mp=new HashMap<String,String>();
mp.put("1", "avinash");
mp.put("2", "venu");
mp.put("3", "dharma");
mp.put("4", "hari");
Iterator<String> it=mp.keySet().iterator();
while(it.hasNext()){
String temp=it.next();
System.out.println(temp + " " +mp.get(temp));
}
}
}
Method 2: Using foreach() method. It is defined as default method in the Map interface.
package seleniumrepo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("1", "avinash");
map.put("2", "venugopal");
map.put("3", "dharma");
map.put("4", "hari");
map.forEach((k,v)->System.out.println("Key: "+k+" Value: "+v));
}
Method 3: Using entrySet() and Map.Entry(K, V).
package javaprograms;
import java.util.HashMap;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("1", "avinash");
map.put("70", "venugopal");
map.put("30", "dharma");
map.put("4", "hari");
System.out.println(map.values());
for(Map.Entry<String, String> entry: map.entrySet()) {
System.out.println("Key: "+entry.getKey()+"\t"+ "Value: "+entry.getValue());
}
}
}
Output:
[avinash, hari, venugopal, dharma]
Key: 1 Value: avinash
Key: 4 Value: hari
Key: 70 Value: venugopal
Key: 30 Value: dharma
0 comments:
Post a Comment