当前位置: 移动技术网 > IT编程>开发语言>Java > java map集合 --遍历

java map集合 --遍历

2018年11月23日  | 移动技术网IT编程  | 我要评论

7k7k小花仙小游戏,云海玉弓缘粤语,温州招聘信息

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();

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网