• 使用lambda表达式对两个list并集、去重并集、交集、差集处理


    import java.util.ArrayList;
    import java.util.List;
    import static java.util.stream.Collectors.toList;
    public class Code {
        public static void main(String[] args) throws ParseException {
            List<String> list1 = new ArrayList<String>();
            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");
            new Code().quchongjiaoji(list1,list2);
        }
    
        // 交集
        public void jiaoji(List<String> list1,List<String> list2){
        List<String> intersection = list1.stream().filter(item ->   list2.contains(item)).collect(toList());
        System.out.println("---交集 intersection---");
        intersection.parallelStream().forEach(System.out :: println);
        }
    
        // 差集 (list1 - list2)
        public void chaji(List<String> list1,List<String> 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)
        public void chajito(List<String> list1,List<String> list2){
        List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
        System.out.println("---差集 reduce2 (list2 - list1)---");
        reduce2.parallelStream().forEach(System.out :: println);
        }
    
        // 并集
        public void binji(List<String> list1,List<String> list2){
        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);
        }
    
        // 去重并集
        public void quchongjiaoji(List<String> list1,List<String> list2){
         List<String> listAll = list1.parallelStream().collect(toList());
         List<String> listAll2 = list2.parallelStream().collect(toList());
         listAll.addAll(listAll2);
         List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
         System.out.println("---得到去重并集 listAllDistinct---");
         listAllDistinct.parallelStream().forEachOrdered(System.out::println);
        }
        }
     
  • 相关阅读:
    Javascript 入门 document
    JavaScript 入门 (一)
    Python 之 Json序列化嵌套类
    [20171113]修改表结构删除列相关问题3.txt
    [20171113]修改表结构删除列相关问题2.txt
    [20171113]修改表结构删除列相关问题.txt
    [20171110]_allow_read_only_corruption参数.txt
    [20171107]dbms_shared_pool.pin补充.txt
    [20171107]dbms_shared_pool.pin.txt
    [20171106]修改show spparameter的显示宽度.txt
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/14119016.html
Copyright © 2020-2023  润新知