【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
方法一 通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时
这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); }
方法二、通过Map.keySet遍历key,通过键找值value遍历(效率低),普遍使用,二次取值
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Integer key : map.keySet()) { Integer value = map.get(key); System.out.println("Key = " + key + ", Value = " + value); }
方法三 如果只需要map中的键或者值,你可以通过Map.keySet或Map.values来实现遍历,而不是用entrySet。在for-each循环中遍历keys或values。
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); //遍历map中的键 for (Integer key : map.keySet()) { System.out.println("Key = " + key); } //遍历map中的值 for (Integer value : map.values()) { System.out.println("Value = " + value); }
方法四:通过Map.entrySet使用iterator遍历key和value
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 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()); }
全部代码:
public static void main(String[] args) {
init();
traversal();
} // HashMap按照哈希算法来存取键对象,有很好的存取性能
private static HashMap<String, String> hMap = new HashMap<>(); // TreeMap实现了SortedMap接口,能对键对象进行排序。支持自然排序和客户化排序两种方式。
private static TreeMap<String, String> tMap = new TreeMap<>(); // map的赋值
public static void init() {
hMap.put("1", "value1");
hMap.put("2", "value2");
hMap.put("3", "value3");
} // map的遍历
public static void traversal() {
// 第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : hMap.keySet()) {
System.out.println("key= " + key + " and value= " + hMap.get(key));
} // 第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = hMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
} // 第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : hMap.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
} // 第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : hMap.values()) {
System.out.println("value= " + v);
}
}
运行结果:
通过Map.keySet遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.entrySet使用iterator遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.entrySet遍历key和value
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.values()遍历所有的value,但不能遍历key
value= value1
value= value2
value= value3
【Java集合的详细研究4】Java中如何遍历Map对象的4种方法的更多相关文章
- (转载)Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 转!! Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 【转】Java中如何遍历Map对象的4种方法
原文网址:http://blog.csdn.net/tjcyjd/article/details/11111401 在Java中如何遍历Map对象 How to Iterate Over a Map ...
- Java中如何遍历Map对象的4种方法
在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...
- Java中遍历Map对象的4种方法
java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等). HashMap<Inte ...
- Java遍历Map对象的四种方法
在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...
- Java 遍历Map对象的4种方法
http://blog.csdn.net/tjcyjd/article/details/11111401
- (转)在Java中如何遍历Map对象
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- Java遍历Map对象的四种方式
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...
随机推荐
- (5)ps详解 (每周一个linux命令系列)
(5)ps详解 (每周一个linux命令系列) linux命令 ps详解 引言:今天的命令是用来看进程状态的ps命令 ps 我们先看man ps ps - report a snapshot of t ...
- 最简单例子图解JVM内存分配和回收(转)
本文转自http://ifeve.com/a-simple-example-demo-jvm-allocation-and-gc/ http://www.idouba.net/a-simple-exa ...
- Python基础(7)——迭代器&生成器
1.列表生成式 [i*2 for i in range(10)] [fun(i) for i in range(10)] 2.生成器 # Author Qian Chenglong #列表生成器 a= ...
- 用Qt程序打开.txt 文件的时候,出现乱码的情况
打开*.txt 文件出现乱码的情况,说明编码格式不对,一般的Windows下创建的txt 文件,到ubuntu 系统中打开会出现乱码的情况, 下面的代码读取文件,并且转化编码格式 void MainW ...
- jsp中使用原生js实现异步交互
Ajax的工作原理相当于在用户和服务器之间加了—个中间层,使用户操作与服务器响应异步化.并不是所有的用户请求都提交给服务器,像—些数据验证和数据处理等都交给Ajax引擎自己来做,只有确定需要从服务器读 ...
- [03-01] JSP自定义标签
1.自定义标签的概念 目前我们在JSP中使用的标签都是HTML的标签,浏览器会自动解析运行,例如<form action=""></form>,这里的for ...
- Luogu3877 TJOI2010 打扫房间 二分图、网络流
传送门 真是菜死了模板题都不会-- 首先\(30 \times 30\)并不能插头DP,但是范围仍然很小所以考虑网络流. 注意每个点都要包含在一个回路中,那么每一个点的度数都必须为\(2\),也就是说 ...
- asp.net网站,在没有项目源码情况下的扩展
如果在没有源码的情况下,要扩展asp.net网站,可以自己新增一个类库项目,在里面添加需要扩展的类,代码如下: using System; using System.Collections.Gener ...
- git 提交新增文件到网站
git add -A 是将所有的修改都提交.你可以用git status查看当前的变化,然后通过git add xxx有选择的提交.git commit 是将变化先提交到本地.git commit - ...
- [Spark][Python]PageRank 程序
PageRank 程序: file contents: page1 page3page2 page1page4 page1page3 page1page4 page2page3 page4 def c ...