Map KeySet方法

阅读更多
package com.map;  
  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Set;  
/**  
 * 遍历: key value   
 *  
 */  
public class KeySetDemo {  
public static void main(String[] args) {  
    Map map = new HashMap();  
    map.put("01", "zhangsan1");  
    map.put("02", "zhangsan2");  
    map.put("03", "zhangsan3");  
    map.put("04", "zhangsan4");  
    Set keySet = map.keySet();  
    //方法1:  
    for (String key : keySet) {  
        System.out.println(key+"--->"+map.get(key));  
    }  
      
    //方法2:  
    for( String key :map.keySet()){  
        System.out.println(key+"-------"+map.get(key));  
    }  
      
    //方法3:  
    Iterator it = keySet.iterator();  
    while(it.hasNext()){  
        String key = it.next();  
        String value = map.get(key);  
        System.out.println(key+"   " +value);  
    }  
}  
}  

你可能感兴趣的:(java)