zoukankan      html  css  js  c++  java
  • Lambda表达式

    去重

    • 两个list对象根据条件去重
    List<User> userList 和 List<Person> personList
    去掉 userList 中的 User 的 id 不等于 personList 中的 Person 的 id 的对象
    List<User> userList = userList.stream()
        .filter(user -> !personList.stream().map(person - >person.getId()).collect(Collectors.toList())
        .contains(user.getId()))
        .collect(Collectors.toList());
        
    list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                    -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new));  
    
    • 简单List集合求交、去重、差集
    // 交集 (parallelStream() 使用并行流遍历,多线程; stream() )
    List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
    // 差集 (list1 - list2)
    List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
    // 差集 (list2 - list1)
    List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
    // 去重
    List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
    

    排序

    List<Person> sortedList = list.stream()
        .sorted((o1,o2) -> o1.getAge() - o2.getAge())
        .collect(Collectors.toList());
    List<Person> sortedList =  list.sort(Comparator.comparing(ClassImagePraise::getSort));    
    List<Person> sortedList =  list.sort(Comparator.comparing(ClassImagePraise::getSort)).reversed());  
    // 多条件组合排序
    // 先根据msg(升序),再根据id(升序)
    list.sort(Comparator.comparing(Message:: getMsg).thenComparing(Message::getId));
    // 根据msg(升序),再根据id(降序)
    list.sort(Comparator.comparing(Message:: getMsg).thenComparing(Comparator.comparing(Message::getId).reversed()));
    // 先根据msg(降序),再根据id(降序)
    list.sort(Comparator.comparing(Message:: getMsg).thenComparing(Message::getId).reversed());
    // 先根据msg(降序),再根据id(升序)
    list.sort(Comparator.comparing(Message:: getMsg).reversed().thenComparing(Message::getId));
    

    过滤

    List<Person> filterList = list.stream().filter(item -> item.getAge()>3).collect(Collectors.toList());
    

    筛选、转换

    List<String> mapList1 = list.stream().map(Person::getName).collect(Collectors.toList());
    List<String> mapList2 = list.stream().map(item -> item.getName()).collect(Collectors.toList());
    // 过滤与筛选结合
    List<String> teacherIds = pList.stream()
        .filter(s -> s.getId().equals("333"))
        .map(p -> p.getUserId())
        .collect(Collectors.toList());
        
    tests.stream().collect(Collectors.toMap(item -> item.getId(), item -> item))    
    

    统计

    getAverage统计 sum() 。mapToDouble() 转换成double。还有其他类型转换。可以自己研究。max(),min(),average()
    double sum = list.stream().mapToDouble(Person::getAge).sum();
    
    Optional<Message> maxMassage = list.stream().collect(Collectors.maxBy(Comparator.comparing(Message::getId)));
    Long maxId = maxMassage.get().getId();
    
    LongSummaryStatistics lss = list.stream().collect(Collectors.summarizingLong(Message::getId));
        //  求和 lss.getSum()
        //  求最大值 lss.getMax()
        // 求最小值 lss.getMin()
        // 求平均值 lss.getAverage()
    

    分组

    Map<Integer, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getAge));
    // 多重分组
    Map<String, Map<Integer, List<Person>>> map2 = list.stream()
        .collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge())));
    // 分组并计算综合        Collectors.summarizingLong()
    Map<String, Map<Integer, LongSummaryStatistics>> map3 = list.stream()
        .collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge(),Collectors.summarizingLong(Person::getSize))));
    
  • 相关阅读:
    C语言(北京理工大学MOOC 上)
    四则运算、进制转换、变量内存分配
    C语言(挑战ACM-ICPC,DEVFORGE学编程社区)
    迭代公式求平方根
    C语言(数据结构,DEVFORGE学编程社区)
    C语言(复杂数据,DEVFORGE学编程社区)
    C语言(字符串,DEVFORGE学编程社区)
    c语言(北京理工大学mooc 下)
    华中农业大学mooc编程题
    Java通过POI读取Excel
  • 原文地址:https://www.cnblogs.com/reroyalup/p/12133047.html
Copyright © 2011-2022 走看看