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);
        
        // CollectionUtils实现
        List<String> listA = new ArrayList<String>();
        listA.add("a");
        listA.add("b");
        listA.add("b");
        listA.add("c");
        List<String> listB = new ArrayList<String>();
        listB.add("a");
        listB.add("e");
        listB.add("f");
        System.out.println("集合A:"+listA);
        System.out.println("集合B:"+listB);
        System.out.println("------------------------");
        //并集
        Collection<String> union = CollectionUtils.union(listA, listB);
        System.out.println("并集:"+union);
        //交集
        Collection<String> intersection = CollectionUtils.intersection(listA, listB);
        System.out.println("交集:"+intersection);
        //交集的补集
        Collection<String> disjunction = CollectionUtils.disjunction(listA, listB);
        System.out.println("交集的补集   :"+disjunction);
        //差集(集合相减)
        Collection<String> subtract = CollectionUtils.subtract(listA, listB);
        System.out.println("差集(集合相减)   :"+subtract);

    }

}

结果:

集合A:[a, b, b, c]
集合B:[a, e, f]
------------------------
并集:[a, b, b, c, e, f]
交集:[a]
交集的补集   :[b, b, c, e, f]
差集(集合相减)   :[b, b, c]

依赖

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2</version>
</dependency>
posted @ 2020-06-17 14:48  itwetouch  阅读(23639)  评论(1编辑  收藏  举报