遍历map的几种方法

public class Test{ 

public static void main(String[] args) {

Map<String,String> map = new HashMap<String,String>(); 

map.put("id1", "wang"); 

map.put("id2", "li"); 

 

//map的遍历方法1 

Set<String> set = map.keySet(); 

for(String s:set){ System.out.println(s + "," + map.get(s)); } 

 

//map的遍历方法2 

Set<Map.Entry<String, String>> entryseSet = map.entrySet(); 

for(Map.Entry<String, String> entry:entryseSet){ System.out.println(entry.getKey()+","+entry.getValue()); } 

 

//map的遍历方法3 

Iterator<String> it = map.keySet().iterator(); 

while(it.hasNext()){ System.out.println(map.get(it.next())); } 

 

//map的遍历方法4 

Iterator it = map.values().iterator(); 

while(it.hasNext()){ 

String val = (String)it.next(); 

System.out.println(val); 

}

}

你可能感兴趣的:(map)