Map结构,在Java应用开发中,经常会用到,那么,Map的遍历方式有那些呢,本文来做个总结。
一、Map介绍
Map是java中的接口,Map.Entry是Map的一个内部接口。Map提供了一些常用方法,如keySet()、entrySet()等方法,keySet()方法返回值是Map中key值的Set集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。
Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry。它表示Map中的一个实体(一个key-value对)。接口中有getKey(),getValue方法。
二、遍历的方法
通过查看Map的API,可以看到Map中,提供多种遍历的方式
- 先用keySet()取出所有key值,再取出对应value——增强for循环遍历
- 先用keySet()取出所有key值,再取出对应value——使用迭代器遍历
- 通过entrySet来获取key-value——增强for循环遍历
- 通过entrySet来获取key-value——使用迭代器遍历
- 通过ForEach来遍历
三、举例说明
1、 构造一个Map结构
首先,先来构造一个Map结构,并在Map中存放如下的数据作为测试
Map<String, Object> maps = new HashMap<>();
maps.put("001", "张三");
maps.put("002", "李四");
maps.put("003", "王五");
maps.put("004", "大白");
maps.put("005", "Jack");
maps.put("006", "Tom");
2、 方法一:使用keySet()取出所有key值,加上for循环
2.1 代码
Set keyset = maps.keySet();
for(Object key : keyset){
log.info("key = {}, value = {}", key, maps.get(key));}
2.2 运行结果
18:04:25.481 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 001, value = 张三
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 002, value = 李四
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 003, value = 王五
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 004, value = 大白
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 005, value = Jack
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 006, value = Tom
3、 方法二:使用keySet()取出所有key值,加上Iterator循环
3.1 代码
Set<String> keySet = maps.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()){
String key = iterator.next();
log.info("key = {}, value = {}", key, maps.get(key));
}
3.2 运行结果
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 001, value = 张三
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 002, value = 李四
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 003, value = 王五
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 004, value = 大白
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 005, value = Jack
18:04:25.500 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 006, value = Tom
4、 方法三:通过entrySet()获得key-value值——增强for循环遍历
4.1 代码
Set set = maps.entrySet();
for(Object key : set){
Map.Entry entry = (Map.Entry) key;
log.info("key = {}, value = {}", entry.getKey(), entry.getValue());
}
4.2 运行结果
18:09:44.849 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 001, value = 张三
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 002, value = 李四
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 003, value = 王五
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 004, value = 大白
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 005, value = Jack
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 006, value = Tom
5、 方法四:通过entrySet()获得key-value值——使用迭代器遍历
5.1 代码
Set entrySet = hashMap.entrySet();
Iterator iterator1 = entrySet.iterator();while(iterator1.hasNext()){
Object itset = iterator1.next();
Map.Entry entry = (Map.Entry) itset;
log.info("key = {}, value = {}", entry.getKey(), entry.getValue());}
5.2 运行结果
18:09:44.849 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 001, value = 张三
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 002, value = 李四
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 003, value = 王五
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 004, value = 大白
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 005, value = Jack
18:09:44.850 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 006, value = Tom
6、 方法四:通过foreach来遍历
6.1 代码
maps.forEach((k, v) ->{
log.info("key = {}, value = {}", k, v);
});
6.2 运行结果
18:15:47.529 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 001, value = 张三
18:15:47.529 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 002, value = 李四
18:15:47.529 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 003, value = 王五
18:15:47.530 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 004, value = 大白
18:15:47.530 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 005, value = Jack
18:15:47.530 [main] INFO net.jhelp.easyql.demo.tests.MapTest - key = 006, value = Tom
以上是基础的Map遍历方法,Java8简化了很多操作,效率上基本无差。