• List 过滤、排序、校验等处理方法


    List filter:过滤、map:映射、sorted:排序、forEach、collect:聚合、statistics:统计、parallelStream:并行流

     
     
    public class Example{
        private String name;
        private Double score;
     
        public Example(String name, Double score) {
            this.name = name;
            this.score = score;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public Double getScore() {
            return score;
        }
     
        public void setScore(Double score) {
            this.score = score;
        }
     
        @Override
        public String toString() {
            return "Example{" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    '}';
        }
    }

    +--------------------+       +------+   +------+   +---+   +-------+
    | stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
    +--------------------+       +------+   +------+   +---+   +-------+
     

    处理测试类

    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.DoubleSummaryStatistics;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
     
     
     
    public class StreamTest {
     
     
        public static void main(String args[]){
     
            List<Example> list = new ArrayList<>();
            list.add(new Example("乔峰", 20.d));
            list.add(new Example("虚竹", 40.d));
            list.add(new Example("段誉", 80.d));
            list.add(new Example("扫地僧", 50.d));
            list.add(new Example("方丈", null));
            list.add(new Example("王语嫣", 30.d));
     
            long count = 0;
            List<Example> filterList = null;
     
     
            // filter 过滤器的使用
            // 筛选出成绩不为空的学生人数
            count = list.stream().filter(p -> null != p.getScore()).count();
            System.out.println("参加考试的学生人数:" + count);
     
            // collect
            // 筛选出成绩不为空的学生集合
            filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList());
            System.out.println("参加考试的学生信息:");
            filterList.stream().forEach(System.out::println);
     
            // map 将集合映射为另外一个集合
            // 取出所有学生的成绩
            List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
            System.out.println("所有学生的成绩集合:" + scoreList);
     
            // 将学生姓名集合串成字符串,用逗号分隔
            String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));
            System.out.println("所有学生的姓名字符串:" + nameString);
     
            // sorted排序
            // 按学生成绩逆序排序 正序则不需要加.reversed()
            filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());
            System.out.println("所有学生的成绩集合,逆序排序:");
            filterList.stream().forEach(System.out::println);
     
            System.out.println("按学生成绩归集:");
            Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore())
                    .collect(Collectors.groupingBy(UserPo::getScore));
            for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
                System.out.println("成绩:" + entry.getKey() + " 人数:" + entry.getValue().size());
            }
     
            // forEach
            filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));
            System.out.println("及格人数太少,给每个人加10分");
            filterList.stream().forEach(System.out::println);
     
            // count
            count = filterList.stream().filter(p -> p.getScore() >= 60).count();
            System.out.println("最后及格人数" + count);
     
            DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics();
            System.out.println("列表中最大的数 : " + statistics.getMax());
            System.out.println("列表中最小的数 : " + statistics.getMin());
            System.out.println("所有数之和 : " + statistics.getSum());
            System.out.println("平均数 : " + statistics.getAverage());
     
            // 并行流 使用
            count = list.parallelStream().filter(p -> null != p.getScore()).count();
            System.out.println("test:" + count);
     
        }
     
    }
  • 相关阅读:
    Web服务器推送技术【转】
    [转]vs2010 中文版下载地址及可用CDKEY
    [php] sae上的一个应用框架申请通过了
    [linux] ssh WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 问题解决
    [php] 调试利器
    [javascript] 邮箱&&电话正则
    [erlang] Erlang比较运算符 (Term Comparisons)
    [vim] gvim 折行
    [linux] 查看内存型号
    [linux] mtu查看&&设置
  • 原文地址:https://www.cnblogs.com/Fooo/p/16717703.html
Copyright © 2020-2023  润新知