遍历Map集合的几种方式

本文介绍了三种使用Java遍历Map集合的方法:通过keySet获取所有键并逐一取值;利用entrySet的迭代器遍历键值对;直接通过Map.Entry遍历Map。每种方法都附带了详细的代码示例。
 1 import java.util.HashMap;
 2 import java.util.Iterator;
 3 import java.util.Map;
 4 import java.util.Map.Entry;
 5 
 6 /**
 7  * <p>遍历Map集合</p>
 8  * @author:774346810@qq.com
 9  * @date:2017-5-30
10  */
11 public class Test {
12     public static void main(String[] args) {
13         Map<String, String> map = new HashMap<String, String>();
14         map.put("username", "yujiwei");
15         map.put("password", "12345");
16         map.put("address", "hangzhou");
17         map.put("love", "编程");
18         //1.获取所有的key
19         for(String key : map.keySet()){//返回的是map的key值
20             String value = map.get(key);//通过key取value
21             System.out.println("key = " + key + ",value = " + value);
22         }
23         
24         System.out.println("----------------------------------");
25         
26         //2.通过map.entrySet的iterator来遍历Map集合
27         Iterator<Entry<String, String>> it = map.entrySet().iterator();
28         while(it.hasNext()){
29             Entry<String, String> entry = it.next();
30             System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
31         }
32         
33         System.out.println("----------------------------------");
34         
35         //3.通过Map.Entry来遍历Map集合
36         for(Map.Entry<String, String> entry : map.entrySet()){
37             System.out.println("key= " + entry.getKey() + " and value= "+ entry.getValue());
38         }
39     }
40 }

 

转载于:https://www.cnblogs.com/yujiwei/p/6921472.html

MyBatis遍历Map集合主要通过`<foreach>`标签实现,以下是几种不同场景下的实现方法: ### 用于更新操作 在更新操作中,可使用`<foreach>`标签遍历Map集合,以更新表中的多个字段。 ```xml <update id="updateMessage" useGeneratedKeys="false" parameterType="java.util.Map"> update link_circuit set <foreach collection="updateMap" item="value" index="key" separator=","> ${key} = #{value} </foreach> where remark=#{updateMap.remark} </update> ``` 这里`collection`指定要遍历Map集合,`index`用于获取Map的键,`item`用于获取Map的值,`separator`指定每个键值对之间的分隔符。通过循环,可将Map中的键值对动态地添加到SQL语句中,实现多字段更新 [^1]。 ### 用于动态排序 在查询操作中,若要根据Map集合中的键值对进行动态排序,可使用如下方式: ```xml <select id="dynamicOrder" resultType="com.p7.demo.model.Person"> select * from person <if test="orderBy != null and orderBy.orderByMap.size > 0"> order by <foreach collection="orderBy.orderByMap" index="key" item="value" separator=","> ${key} ${value} </foreach> </if> </select> ``` 在这个例子中,通过`<if>`标签判断Map集合是否存在且不为空。若条件满足,使用`<foreach>`标签遍历Map集合,将键值对添加到`order by`子句中,实现动态排序 [^2]。 ### 用于多条件查询 在多条件查询场景中,可遍历Map集合中的键值对,将其作为查询条件: ```xml <select id="getCountAlarmSignalByCondition" parameterType="AlarmSignalConditionDTO" resultType="Integer"> SELECT count(*) FROM ( <foreach collection="intervalTimeMap.keys" item="key" separator=" union all "> SELECT * FROM( SELECT * FROM t_external_entrance_alarm_signal WHERE <![CDATA[ occur_time >= #{intervalTimeMap[${key}].startTime} AND occur_time < #{intervalTimeMap[${key}].endTime} ]]> and alarm_grade = #{alarmGrade} and acs_id = #{acsId} and entrance_id = #{objectId} )${key} </foreach> )t </select> ``` 这里通过遍历`intervalTimeMap`的键,将每个键对应的对象的属性值作为查询条件,使用`union all`将多个查询结果合并 [^3]。 ### 遍历Map中的List 若Map集合中的值是List类型,可使用如下方式遍历: ```xml <if test="u.mdx!=null and u.mdx.size() >0 "> and km.mdx_id in <foreach collection="u.mdx" item="mdxId" index="index" open="(" close=")" separator=","> #{mdxId} </foreach> </if> ``` 在这个例子中,先通过`<if>`标签判断Map中的List是否存在且不为空。若满足条件,使用`<foreach>`标签遍历List,将List中的元素添加到`in`子句中 [^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值