java8合并两个Map

x33g5p2x  于2022-01-13 转载在 Java  
字(3.4k)|赞(0)|评价(0)|浏览(460)

合并两个Map

map自己的方法
实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式。

  1. public static void main(String[] args) {
  2. Map<String, String> map1 = new HashMap<String, String>();
  3. map1.put("one", "一");
  4. map1.put("two", "二");
  5. map1.put("three", "三");
  6. Map<String, String> map2 = new HashMap<String, String>();
  7. map1.put("ten", "十");
  8. map1.put("nine", "九");
  9. map1.put("eight", "八");
  10. // 合并
  11. Map<String, String> combineResultMap = new HashMap<String, String>();
  12. combineResultMap.putAll(map1);
  13. combineResultMap.putAll(map2);
  14. // 合并后打印出所有内容
  15. for (Map.Entry<String, String> entry : combineResultMap.entrySet()) {
  16. System.out.println(entry.getKey() + ":" + entry.getValue());
  17. }
  18. }

java8合并两个Map

需求:如果两个Map有冲突则Value取第二个Map中的Value

例子准备

实体类

  1. import lombok.*;
  2. @Getter
  3. @Setter
  4. @ToString
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. public class Employee {
  8. private Long id;
  9. private String name;
  10. }

新建两个Map<String,Employee>

  1. private static Map<String, Employee> map1 = new HashMap<>();
  2. private static Map<String, Employee> map2 = new HashMap<>();
  3. static {
  4. Employee employee1 = new Employee(1L, "Henry");
  5. map1.put(employee1.getName(), employee1);
  6. Employee employee2 = new Employee(22L, "Annie");
  7. map1.put(employee2.getName(), employee2);
  8. Employee employee3 = new Employee(8L, "John");
  9. map1.put(employee3.getName(), employee3);
  10. Employee employee4 = new Employee(2L, "George");
  11. map2.put(employee4.getName(), employee4);
  12. Employee employee5 = new Employee(3L, "Henry");
  13. map2.put(employee5.getName(), employee5);
  14. }

在java8之前我们合并

  1. @Test
  2. public void fun0() {
  3. Map<String, Employee> map3 = new HashMap<>(map1);
  4. for (Map.Entry<String, Employee> entry : map2.entrySet()) {
  5. if (map3.containsKey(entry.getKey())) {
  6. map3.put(entry.getKey(), new Employee(map3.get(entry.getKey()).getId(), entry.getValue().getName()));
  7. }else{
  8. map3.put(entry.getKey(), entry.getValue());
  9. }
  10. }
  11. }

java8之后

1.用map提供的merge()方法来合并

  1. @Test
  2. public void fun1() {
  3. Map<String, Employee> map3 = new HashMap<>(map1);
  4. map2.forEach((key, value) -> map3.merge(key, value, (v1, v2) -> new Employee(v1.getId(),v2.getName())));
  5. map3.forEach((k,v)-> System.out.println(k+"=:"+v));
  6. }

merge方法有三个参数,第一个是要合并的Key,第二个是合并的Value,第三个是一个接收两个参数的函数,它的作用就如我们上面的对于重复的key处理的逻辑一样,你可以自定义,v1是map2中的值,v2是map3中重复的值。

2.用Stream.concat来合并

Stream.contcat接收两个值,顺序的将两个集合中的数据加入流中,然后再用收集器变成Map

  1. @Test
  2. public void fun2() {
  3. Stream<Map.Entry<String, Employee>> concat = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream());
  4. Map<String, Employee> collect = concat.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> new Employee(value1.getId(), value1.getName())));
  5. collect.forEach((k,v)-> System.out.println(k+"=:"+v));
  6. }

3.用Stream.of()创建流后合并

和contact不同的是stream.of可以初始化多个元素,然后用扁平化的处理成需要的流,然后用收集器来转为Map

  1. @Test
  2. public void fun3() {
  3. Map<String, Employee> collect = Stream.of(map1, map2)
  4. .flatMap(x -> x.entrySet().stream())
  5. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> new Employee(v1.getId(), v2.getName())));
  6. collect.forEach((k,v)-> System.out.println(k+"=:"+v));
  7. }

4.直接用Collector来收集

  1. @Test
  2. public void fun4() {
  3. Map<String, Employee> map3 = map2.entrySet()
  4. .stream()
  5. .collect(Collectors.toMap(
  6. Map.Entry::getKey,
  7. Map.Entry::getValue,
  8. (v1, v2) -> new Employee(v1.getId(), v2.getName()),() -> new HashMap<>(map1)));
  9. map3.forEach((k,v)-> System.out.println(k+"=:"+v));
  10. }

相关文章

最新文章

更多