package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import static java.util.stream.Collectors.toList;
/**
- @author xuezj
@date 2022/8/10 16:38
*/
public class Test {public static void main(String[] args) {
List<String> list1 = new ArrayList<>(); list1.add("1"); list1.add("2"); list1.add("3"); list1.add("5"); list1.add("6"); List<String> list2 = new ArrayList<>(); list2.add("2"); list2.add("3"); list2.add("7"); list2.add("8"); System.out.print("List1-->"); list1.parallelStream().forEachOrdered(System.out::print); System.out.println(); System.out.print("List2-->"); list2.parallelStream().forEachOrdered(System.out::print); System.out.println(); // 交集 List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList()); System.out.print("交集-->"); intersection.parallelStream().forEach(System.out::print); System.out.println(); // 差集 (list1 - list2) List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList()); System.out.print("差集(list1 - list2)-->"); reduce1.parallelStream().forEach(System.out::print); System.out.println(); // 差集 (list2 - list1) List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList()); System.out.print("差集(list2 - list1)-->"); reduce2.parallelStream().forEach(System.out::print); System.out.println(); // 并集 List<String> listAll = list1.parallelStream().collect(toList()); List<String> listAll2 = list2.parallelStream().collect(toList()); listAll.addAll(listAll2); System.out.print("并集-->"); listAll.parallelStream().forEachOrdered(System.out::print); System.out.println(); // 去重并集 List<String> listAllDistinct = listAll.stream().distinct().collect(toList()); System.out.print("并集去重-->"); listAllDistinct.parallelStream().forEachOrdered(System.out::print);
}
}
输出结果:
List1-->12356
List2-->2378
交集-->32
差集(list1 - list2)-->561
差集(list2 - list1)-->87
并集-->123562378
并集去重-->1235678