实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个
实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式。
public static void main(String[] args) {
Map<String, String> map1 = new HashMap<String, String>();
map1.put("one", "一");
map1.put("two", "二");
map1.put("three", "三");
Map<String, String> map2 = new HashMap<String, String>();
map1.put("ten", "十");
map1.put("nine", "九");
map1.put("eight", "八");
// 合并
Map<String, String> combineResultMap = new HashMap<String, String>();
combineResultMap.putAll(map1);
combineResultMap.putAll(map2);
// 合并后打印出所有内容
for (Map.Entry<String, String> entry : combineResultMap.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
合并后的 map 对象打印结果如下:


相关推荐
-
Sring Data JPA使用Predicate进行模糊查询、按时间段查询
10 : demoDTO.getSize();PageRequest pageRequest = new PageRequest;Specification<W
-
Thymeleaf——th:each遍历Map List
@RequestMappingpublic Object a3{Map<String,User> map1=new HashedMap();User user1
-
js与thymeleaf结合,js获取thymeleaf变量
<tr th:each=\"book : ${books}\"> <td th:text=\"${book.author}\"></td>