遍历map的key和value

public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<String,Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        //entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry;接口中有getKey(),getValue方法
        for(Map.Entry<String,Integer> eny : map.entrySet() ){
            System.out.println(eny.getKey()+"====="+eny.getValue());
        }
        for(String enn : map.keySet()){//遍历key
            System.out.println("key"+"==="+enn);
        }
        for(Integer enn : map.values()){//遍历value
            System.out.println("value"+"==="+enn);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_23927391/article/details/79651493