合并两个Map
需求:如果两个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);
}
在java8之前我们合并
@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());
}
}
}
java8之后
1.用map提供的merge()方法来合并
@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中重复的值。
2.用Stream.concat来合并
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));
}
3.用Stream.of()创建流后合并
和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));
}
4.直接用Collector来收集
@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));
}