码迷,mamicode.com
首页 > 其他好文 > 详细

遍历Map的三种方法

时间:2018-09-28 19:17:05      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:遍历   keyset   shm   推荐   key   hash   利用   ring   迭代器   

Map<String,Object> map = new HashMap<>();
map.put("1",1);
map.put("2",2);
map.put("3",3);
// 第一种遍历,根据keySet()方法
System.out.println("第一种遍历方法:");
for(String key : map.keySet()) {
Object obj = map.get(key);
System.out.println(obj);
}

// 第二种遍历,利用迭代器map.entrySet().iterator()
System.out.println("第二种遍历方法:");
Iterator<Map.Entry<String,Object>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String,Object> entry = entryIterator.next();
System.out.println("entry.getKey():" + entry.getKey());
System.out.println("entry.getValue()" + entry.getValue());
}

// 第三种遍历方法,推荐,简便且合适大容量数值
System.out.println("第三种遍历方法:");
for(Map.Entry<String,Object> entry : map.entrySet()) {
System.out.println("entry.getkey():" + entry.getValue());
}

遍历Map的三种方法

标签:遍历   keyset   shm   推荐   key   hash   利用   ring   迭代器   

原文地址:https://www.cnblogs.com/xiaoyu666/p/9719862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!