java8 lambda表达式 集合交集,去交集

Posted on 2019-07-04 17:45  豆豆2018  阅读(3537)  评论(0编辑  收藏  举报
 ArrayList<Integer> listA = new ArrayList<>();
 ArrayList<Integer> listB = new ArrayList<>();
listA.add(2);
listA.add(3);
listA.add(8);
System.out.println("**this is listA*********"+listA);
 listB.add(3);
        listB.add(8);
        listB.add(9);
        System.out.println("**this is listB*********"+listB);
List<Integer> intersectionList =listA.stream().filter(t->listB.contains(t)).collect(Collectors.toList());
        System.out.println("集合的交集"+intersectionList);

        List<Integer> ListARomoveB =listA.stream().filter(t-> !listB.contains(t)).collect(Collectors.toList());
        System.out.println("a去除b的差集"+ListARomoveB);

        List<Integer> ListBRomoveA =listB.stream().filter(t-> !listA.contains(t)).collect(Collectors.toList());
        System.out.println("b去除a的差集"+ListBRomoveA);