首页 > 其他 > 详细

map的遍历方式

时间:2017-12-28 20:51:29      阅读:231      评论:0      收藏:0      [点我收藏+]

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, "abc");

1、
for(Integer in:map.keySet()) {
String s = map.get(in);
System.out.println(s);
}

2、使用迭代器
Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Entry<Integer, String> entry = iterator.next();
System.out.println(entry.getKey());
}

3、适合大数据量
for (Entry<Integer, String> e : map.entrySet()) {
System.out.println(e.getValue());
}

4、顺序读取map的值
for (String v : map.values()) {
System.out.println(v);
}

map的遍历方式

原文:https://www.cnblogs.com/ouyangping/p/8137456.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!