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 IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用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'