Java8 stream中的toMap、groupBy、sorted、findFirst、findAny、min、max

Java8 stream中的toMap、groupBy、sorted、findFirst、findAny、min、max的说明与使用示例

1、toMap:转为Map。

2、groupBy:分组。

3、sorted:排序。

4、findFirst、findAny:查找指定数据。

5、min、max:极值。

使用示例
package com.cfay.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lcb
 * @since 2022/7/17
 */
public class StreamDemo {

    public static void main(String[] args) {
        //数据准备
        List<Student> studentList = buildStudent();
        //1、toMap
        toMap(studentList);
        //2、groupBy操作
        groupBy(studentList);
        //3、排序
        sorted(studentList);
        //4、findFirst、findAny
        findFirstOrAny(studentList);
        //5、min、max
        minOrMax(studentList);
    }

    private static void toMap(List<Student> studentList) {
        printTip("toMap");
        //根据id获取对应的值
        Map<Long, Student> studentMap = studentList.stream()
                .collect(Collectors.toMap(Student::getId, Function.identity()));
        studentMap.forEach((index, student) -> System.out.println(student));
        dividingLine();
    }

    private static void groupBy(List<Student> studentList) {
        printTip("groupBy");
        //根据分数值获取对应的人员信息
        Map<BigDecimal, List<Student>> scoreMap = studentList.stream().collect(Collectors.groupingBy(Student::getScore));
        scoreMap.forEach((index, students) -> students.forEach(System.out::println));
        dividingLine();
    }

    private static void sorted(List<Student> studentList) {
        printTip("sorted");
        printTip("①按照分数进行升序排列");
        //①按照分数进行升序排列
        printTip("方式一");
        //方式一
        List<Student> orderByScoreAsc1 = studentList.stream().sorted(Comparator.comparing(Student::getScore))
                .collect(Collectors.toList());
        orderByScoreAsc1.forEach(System.out::println);
        dividingLine();
        printTip("方式二");
        //方式二
        List<Student> orderByScoreAsc2 = studentList.stream().sorted(Comparator.comparing(Student::getScore))
                .collect(Collectors.toList());
        orderByScoreAsc2.forEach(System.out::println);
        dividingLine();
        printTip("②按照分数进行倒序排列");
        //②按照分数进行倒序排列
        printTip("方式一");
        //方式一
        List<Student> orderByScoreDesc1 = studentList.stream().sorted(Comparator.comparing(Student::getScore,
                Comparator.reverseOrder())).collect(Collectors.toList());
        orderByScoreDesc1.forEach(System.out::println);
        dividingLine();
        printTip("方式二");
        //方式二
        List<Student> orderByScoreDesc2 = studentList.stream().sorted(Comparator.comparing(Student::getScore)
                .reversed()).collect(Collectors.toList());
        orderByScoreDesc2.forEach(System.out::println);
        dividingLine();
        printTip("多属性排序");
        //多属性排序
        List<Student> moreStudents = studentList.stream().sorted(Comparator.comparing(Student::getId)
                .thenComparing(Student::getName).reversed()
                .thenComparing(Student::getScore, Comparator.reverseOrder())).collect(Collectors.toList());
        moreStudents.forEach(System.out::println);
        dividingLine();
    }

    private static void findFirstOrAny(List<Student> studentList) {
        printTip("findFirstOrAny");
        //发现一个就返回、发现任意一个就返回,否则就new一个,在同步流的情况下,findFirst和findAny的结果是一样的,
        // 只有在异步流的情况下,findFirst、findAny才会有不同的结果出现
        Student studentFirst = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
                .findFirst().orElseGet(Student::new);
        System.out.println("studentFirst = " + studentFirst);
        Student studentAny = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
                .findFirst().orElseGet(Student::new);
        System.out.println("studentAny = " + studentAny);
        dividingLine();
    }

    private static void minOrMax(List<Student> studentList) {
        printTip("minOrMax");
        //min
        Student studentMin = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
                .min(Comparator.comparing(Student::getId)).orElse(null);
        System.out.println("studentMin = " + studentMin);
        //max
        Student studentMax = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
                .max(Comparator.comparing(Student::getId)).orElse(null);
        System.out.println("studentMax = " + studentMax);
        dividingLine();
    }

    public static List<Student> buildStudent() {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student(1L, "22", BigDecimal.valueOf(88L)));
        studentList.add(new Student(2L, "33", BigDecimal.valueOf(89L)));
        studentList.add(new Student(3L, "44", BigDecimal.valueOf(80L)));
        studentList.add(new Student(4L, "55", BigDecimal.valueOf(84L)));
        studentList.add(new Student(5L, "66", BigDecimal.valueOf(88L)));
        studentList.add(new Student(6L, "77", BigDecimal.valueOf(90L)));
        studentList.add(new Student(7L, "88", BigDecimal.valueOf(95L)));
        studentList.add(new Student(8L, "99", BigDecimal.valueOf(99L)));
        return studentList;
    }

    private static void dividingLine() {
        System.out.println("------------------------------------");
    }

    private static void printTip(String tip) {
        System.out.println(tip + " start ...");
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class Student {

        private Long id;

        private String name;

        private BigDecimal score;
    }
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚风岸影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值