JDK1.8 stream流再会
一、最大值、最小值、平均值、个数
import lombok.Data;import lombok.NoArgsConstructor;import java.util.*;import java.util.stream.Collectors;@Data@NoArgsConstructorpublic class Dish { private Double calories; public Dish(Double calories) { this.calories = calories; } public static void main(String[] args) { List list = new ArrayList<>(); list.add(new Dish(100.0)); list.add(new Dish(200.0)); list.add(new Dish(300.0)); //查找流中的最大值和最小值 Optional max1 = list.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories))); Optional min2 = list.stream().min(Comparator.comparing(Dish::getCalories)); max1.ifPresent(dish -> System.out.println("最大值:" + dish.getCalories())); min2.ifPresent(dish -> System.out.println("最小值:" + dish.getCalories())); //求和 Double sum1 = list.stream().collect(Collectors.summingDouble(Dish::getCalories)); Double sum2 = list.stream().mapToDouble(Dish::getCalories).sum(); System.out.println("求和:" + sum1); System.out.println("求和:" + sum2); //求平均 Double avg1 = list.stream().collect(Collectors.averagingDouble(Dish::getCalories)); System.out.println("平均值:" + avg1); //总体分析 DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Dish::getCalories)); //分析结果DoubleSummaryStatistics{count=3, sum=600.000000, min=100.000000, average=200.000000, max=300.000000} System.out.println("分析结果" + statistics); System.out.println("最大" + statistics.getMax()); System.out.println("最小" + statistics.getMin()); System.out.println("平均" + statistics.getAverage()); System.out.println("总和" + statistics.getSum()); System.out.println("个数" + statistics.getCount()); }}
二、分区
//超过150卡路里的算true,否则算false Predicate predicate = (Dish e) -> e.getCalories().compareTo(150.0) > 0; Map> map = list.stream().collect(Collectors.partitioningBy(predicate)); //分区后:{false=[Dish(calories=100.0)], true=[Dish(calories=200.0), Dish(calories=300.0)]} System.out.println("分区后:" + map);
分组带排序
使用LinkedHashMap而不是Map接口接收最后的分组结果。
三、排序
@Data@NoArgsConstructorpublic class Dish { private Double calories; private String name; public Dish(Double calories) { this.calories = calories; } public Dish(Double calories, String name) { this.calories = calories; this.name = name; } public static void main(String[] args) { List list = new ArrayList<>(); list.add(new Dish(100.0, "A")); list.add(new Dish(200.0, "C")); list.add(new Dish(300.0, "B")); list.add(new Dish(400.0, "A")); //提取出名称:[A, C, B, A] List names = list.stream().map(Dish::getName).collect(Collectors.toList()); System.out.println("提取出名称" + names); //按照名称排序:按照名称排序 // [ // Dish(calories=100.0, name=A), // Dish(calories=400.0, name=A), // Dish(calories=300.0, name=B), // Dish(calories=200.0, name=C) // ] List names2 = list.stream().sorted(Comparator.comparing(Dish::getName)).collect(Collectors.toList()); System.out.println("按照名称排序" + names2); //按照名称倒序[C, B, A, A] List names3 = list.stream() .sorted(Comparator.comparing(Dish::getName).reversed()) .map(Dish::getName).collect(Collectors.toList()); System.out.println("按照名称倒序" + names3);
四、截断
@Data@NoArgsConstructorpublic class Dish { private Double calories; private String name; public Dish(Double calories) { this.calories = calories; } public Dish(Double calories, String name) { this.calories = calories; this.name = name; } public static void main(String[] args) { List list = new ArrayList<>(); list.add(new Dish(100.0, "A")); list.add(new Dish(200.0, "C")); list.add(new Dish(300.0, "B")); list.add(new Dish(400.0, "A")); //截断前2个[A, C] List twoNameList = list.stream().limit(2).map(Dish::getName).collect(Collectors.toList()); System.out.println("截断前2个" + twoNameList); //忽略两个后卡路里之和700.0 Double skipTwoSum = list.stream().skip(2).collect(Collectors.summingDouble(Dish::getCalories)); System.out.println("忽略两个后卡路里之和" + skipTwoSum);
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~