求两个相同类型的List集合的交集、差集、并集

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author ssy
 * @date 2021/7/11 13:46
 */
public class test {

    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add("1");
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("5");
        list1.add("6");

        List<String> list2 = new ArrayList<String>();
        list2.add("2");
        list2.add("3");
        list2.add("7");
        list2.add("8");
        
        System.out.println("List1===========>");
        list1.parallelStream().forEachOrdered(System.out :: println);
        System.out.println("List2===========>");
        list2.parallelStream().forEachOrdered(System.out :: println);

		//         交集
        List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
        System.out.println("交集 intersection===========>");
        intersection.parallelStream().forEach(System.out :: println);

		//         差集 
        List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
        System.out.println("差集reduce1 ===========>");
        reduce1.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);



    }

}

上一篇:删除列表元素


下一篇:关于sqlalchemy读写mysql密码含有【@】,以及中文读写错误的解决