关于map的一些遍历方法
来源:互联网 发布:mac不要开机密码 编辑:程序博客网 时间:2024/06/10 18:03
HashMap是我们日常中经常用法的集合类,它的遍历方法主要分为以下几类:
public class TestMap {
public static void main(String[] args) {Map<Integer, Integer> map = new HashMap<>();
map.put(1, 11);map.put(2, 22);map.put(3, 33);map.put(4, 44);map.put(5, 55);map.put(6, 66);
//Entry遍历(Entry里面有存储键值对,直接获取就行了)
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
//遍历map中的键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
//Iterator遍历,可以在遍历时调用iterator.remove()来删除entries
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
//通过键找值遍历(效率低)
for (Integer key : map.keySet()) {
Integer value = map.get(key);
System.out.println("Key = " + key + ", Value = " + value);
}
}
}
说明:
1.主要遍历的方式还是归根结底部获取set集合(可能是键/值/键值的集合)再遍历。
2.set集合的获取一种是map实例方法获取键值对,另一种就是map的属性Entry来获取键值对集合。
0 0
- 关于map的一些遍历方法
- 遍历Map的一些方法
- 关于map的遍历
- 2017-5-6关于Map集合的遍历一些记录
- Map的遍历方法
- map的遍历方法
- map的遍历方法
- Map的遍历方法
- Map的遍历方法
- Map的遍历方法
- map遍历的方法
- Map的遍历方法
- Map的遍历方法
- 遍历map的方法
- 遍历Map的方法
- Map遍历的方法
- 遍历map的方法
- map的遍历方法
- 手游录屏直播技术详解 | 直播 SDK 性能优化实践
- JSON web token@04#JWT Claims
- Spring Batch 之 Sample(XML文件操作)(五)
- 火车购票系统
- JSON web token@05#JOSE Header
- 关于map的一些遍历方法
- 动态规划----最长上升子序列问题
- RTMP 协议学习总结
- android studio module引用本地aar
- JSON web token@06#Example Unsecured JWT
- Android学习笔记——Activity之间传递数据
- Spring Batch 之 Sample(固定长格式文件读写)(六)
- 后台返回状态码,js验证跳转登录
- mysql中主外键关系