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有冲突则Value取第二个Map中的Value
实体类
import lombok.*;
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private Long id;
private String name;
}
新建两个Map<String,Employee>
private static Map<String, Employee> map1 = new HashMap<>();
private static Map<String, Employee> map2 = new HashMap<>();
static {
Employee employee1 = new Employee(1L, "Henry");
map1.put(employee1.getName(), employee1);
Employee employee2 = new Employee(22L, "Annie");
map1.put(employee2.getName(), employee2);
Employee employee3 = new Employee(8L, "John");
map1.put(employee3.getName(), employee3);
Employee employee4 = new Employee(2L, "George");
map2.put(employee4.getName(), employee4);
Employee employee5 = new Employee(3L, "Henry");
map2.put(employee5.getName(), employee5);
}
@Test
public void fun0() {
Map<String, Employee> map3 = new HashMap<>(map1);
for (Map.Entry<String, Employee> entry : map2.entrySet()) {
if (map3.containsKey(entry.getKey())) {
map3.put(entry.getKey(), new Employee(map3.get(entry.getKey()).getId(), entry.getValue().getName()));
}else{
map3.put(entry.getKey(), entry.getValue());
}
}
}
@Test
public void fun1() {
Map<String, Employee> map3 = new HashMap<>(map1);
map2.forEach((key, value) -> map3.merge(key, value, (v1, v2) -> new Employee(v1.getId(),v2.getName())));
map3.forEach((k,v)-> System.out.println(k+"=:"+v));
}
merge方法有三个参数,第一个是要合并的Key,第二个是合并的Value,第三个是一个接收两个参数的函数,它的作用就如我们上面的对于重复的key处理的逻辑一样,你可以自定义,v1是map2中的值,v2是map3中重复的值。
Stream.contcat接收两个值,顺序的将两个集合中的数据加入流中,然后再用收集器变成Map
@Test
public void fun2() {
Stream<Map.Entry<String, Employee>> concat = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream());
Map<String, Employee> collect = concat.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> new Employee(value1.getId(), value1.getName())));
collect.forEach((k,v)-> System.out.println(k+"=:"+v));
}
和contact不同的是stream.of可以初始化多个元素,然后用扁平化的处理成需要的流,然后用收集器来转为Map
@Test
public void fun3() {
Map<String, Employee> collect = Stream.of(map1, map2)
.flatMap(x -> x.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> new Employee(v1.getId(), v2.getName())));
collect.forEach((k,v)-> System.out.println(k+"=:"+v));
}
@Test
public void fun4() {
Map<String, Employee> map3 = map2.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(v1, v2) -> new Employee(v1.getId(), v2.getName()),() -> new HashMap<>(map1)));
map3.forEach((k,v)-> System.out.println(k+"=:"+v));
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43842093/article/details/122443213
内容来源于网络,如有侵权,请联系作者删除!