Map的遍历方式

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class AboutMap {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
          map.put("1", "value1");
          map.put("2", "value2");
          map.put("3", "value3");
          
        for (Map.Entry<String, String> entry : map.entrySet()) { //通过遍历map对象的入口集合获取其键和值
            System.out.println("key:"+entry.getKey()+"value:"+entry.getValue());
        } 
        
        for (String key : map.keySet()) {  //通过遍历map对象的键获取其键和值
            System.out.println("key:"+key+"value:"+map.get(key));
        }
        
        for (Iterator<Entry<String, String>> iterator = map.entrySet().iterator(); iterator.hasNext();) { //同遍历入口集合
            Entry<String, String> e = (Entry<String, String>) iterator.next();
            System.out.println("key:"+e.getKey()+"value:"+e.getValue());
        }
    }
}

 

posted @ 2016-07-30 14:29  如果屈原会编程  阅读(602)  评论(0编辑  收藏  举报