明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1275, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   且行且思  阅读(189)  评论(0编辑  收藏  举报

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);
 
    }
 
}
复制代码

 

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-09-21 SqlServer :实现树形结构递归查询(无限极分类)
2018-09-21 MySQL:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
点击右上角即可分享
微信分享提示