zoukankan      html  css  js  c++  java
  • Java8两个集合(List)取交集、并集、差集、去重

    Java8两个集合(List)取交集、并集、差集、去重并集

    import java.util.ArrayList;
    import java.util.List;
    
    import static java.util.stream.Collectors.toList;
    
    /**
     * @author ming
     * @version 1.0.0
     * @date 2020/6/17 14:44
     **/
    public class CollectionsTest {
        public static void main(String[] args) {
            List<String> list1 = new ArrayList<>();
            list1.add("1");
            list1.add("2");
            list1.add("3");
            list1.add("4");
            list1.add("5");
    
            List<String> list2 = new ArrayList<>();
            list2.add("2");
            list2.add("3");
            list2.add("6");
            list2.add("7");
    
            // 交集
            List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
            System.out.println("---交集 intersection---");
            intersection.parallelStream().forEach(System.out::println);
    
            // 差集 (list1 - list2)
            List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
            System.out.println("---差集 reduce1 (list1 - list2)---");
            reduce1.parallelStream().forEach(System.out::println);
    
            // 差集 (list2 - list1)
            List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
            System.out.println("---差集 reduce2 (list2 - list1)---");
            reduce2.parallelStream().forEach(System.out::println);
    
            // 并集
            List<String> listAll = list1.parallelStream().collect(toList());
            List<String> listAll2 = list2.parallelStream().collect(toList());
            listAll.addAll(listAll2);
            System.out.println("---并集 listAll---");
            listAll.parallelStream().forEachOrdered(System.out::println);
    
            // 去重并集
            List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
            System.out.println("---得到去重并集 listAllDistinct---");
            listAllDistinct.parallelStream().forEachOrdered(System.out::println);
    
        }
    
    }
    
  • 相关阅读:
    【转】Google 的眼光
    【转】不要去SeaWorld
    【转】Tesla Autopilot
    【转】Tesla Model X的车门设计问题
    【转】Tesla Model S的设计失误
    【转】编程的智慧
    【转】智商的圈套
    【转】创造者的思维方式
    【转】恶评《星际穿越》
    【转】谈创新
  • 原文地址:https://www.cnblogs.com/jockming/p/13152385.html
Copyright © 2011-2022 走看看