JDK1.8 stream流再会

一、最大值、最小值、平均值、个数

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class Dish {

    private Double calories;

    public Dish(Double calories) {
        this.calories = calories;
    }

    public static void main(String[] args) {

        List<Dish> list = new ArrayList<>();
        list.add(new Dish(100.0));
        list.add(new Dish(200.0));
        list.add(new Dish(300.0));

        //查找流中的最大值和最小值
        Optional<Dish> max1 = list.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)));
        Optional<Dish> 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<Dish> predicate = (Dish e) -> e.getCalories().compareTo(150.0) > 0;
        Map<Boolean, List<Dish>> 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);

三、排序

@Data
@NoArgsConstructor
public 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<Dish> 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<String> 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<Dish> names2 = list.stream().sorted(Comparator.comparing(Dish::getName)).collect(Collectors.toList());
        System.out.println("按照名称排序" + names2);

        //按照名称倒序[C, B, A, A]
        List<String> names3 = list.stream()
                .sorted(Comparator.comparing(Dish::getName).reversed())
                .map(Dish::getName).collect(Collectors.toList());
        System.out.println("按照名称倒序" + names3);

四、截断

@Data
@NoArgsConstructor
public 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<Dish> 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<String> 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);

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页