java map集合 --遍历

1.Map 遍历:

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "ab");
map.put(4, "ab");
map.put(4, "ab");// 和上面相同 , 会自己筛选
System.out.println(map.size());
// 第一种:
System.out.println("第一种:通过Map.keySet遍历key和value:");
for (Integer in : map.keySet()) {
    //map.keySet()返回的是所有key的值
    String str = map.get(in);//得到每个key多对用value的值
    System.out.println(in + "     " + str);
}
// 第二种:
System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<Integer, String> entry = it.next();
    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第三种:推荐,尤其是容量大时
System.out.println("第三种:通过Map.entrySet遍历key和value");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
    System.out.println("key= " + entry.getKey() + " and value= "+ entry.getValue());
}
// 第四种:
System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
    System.out.println("value= " + v);
}

2.map的长度:

  int size=Map.size();

猜你喜欢

转载自www.cnblogs.com/yysbolg/p/10005479.html