黑马程序员技术交流社区

标题: Map集合中如何根据键来找到对应的值 [打印本页]

作者: 达达123    时间: 2016-11-10 19:40
标题: Map集合中如何根据键来找到对应的值

HashMap<String, Integer> hm = new HashMap<>();
hm.put("张三", 23);
hm.put("李四", 24);
hm.put("王五", 25);
hm.put("赵六", 26);

/*Set<String> keySet = hm.keySet();         //获取集合中所有的键
Iterator<String> it = keySet.iterator();    //获取迭代器
while(it.hasNext()) {                       //判断单列集合中是否有元素
    String key = it.next();                 //获取集合中的每一个元素,其实就是双列集合中的键
    Integer value = hm.get(key);            //根据键获取值
    System.out.println(key + "=" + value);  //打印键值对
}*/

for(String key : hm.keySet()) {             //增强for循环迭代双列集合第一种方式
    System.out.println(key + "=" + hm.get(key));
}
复制代码
作者: 15626187339    时间: 2016-11-10 20:54
学习 学习




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2