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); } }