面试官:会用stream流找最大值么?我会!

目录

Stream流的聚合和收集

1. Stream的聚合操作count、max

2. Stream的分组操作partitioningBy

代码示例

计数(count):统计语文分数大于70的学生人数

最大值(max):找出数学分数最高的学生姓名

分组(partitioningBy):学生的语文分数是否大于80分

总结


Stream流的聚合和收集

        继续Stream流的两个重要概念:聚合和收集。这些是在处理集合数据时非常强大的工具,能够更轻松地进行数据操作。

1. Stream的聚合操作count、max

        在Stream流中,count()方法用于计算流中元素的数量,而max()方法用于获取流中的最大元素。这些函数将集合的元素聚合成单一的结果。

2. Stream的分组操作partitioningBy

        分组函数是一类用于根据某个条件将元素分为不同组的操作。partitioningBy()方法是Stream流中的一种分组函数,它根据指定的条件将流中的元素分为满足条件和不满足条件两组。这种方式产生的结果是一个Map对象,其中包含两个键值对,分别对应满足条件和不满足条件的元素集合。

代码示例

        实际操作一下。现在,我们将通过一个学生实体类,其中包含年龄、姓名和四门学科的分数。

public class Student {
    private String name;
    private int age;
    private int chineseScore;
    private int mathScore;
    private int englishScore;
    private int physicsScore;
    
    // 构造函数和getter省略
}

计数(count):统计语文分数大于70的学生人数

// 使用for循环
int count = 0;
for (Student student : studentList) {
    if (student.getChineseScore() > 70) {
        count++;
    }
}

// 使用Stream流
long count = studentList.stream()
    .filter(student -> student.getChineseScore() > 70)
    .count();

for循环需要创建一个变量,一个一个得加才能得出,stream流直接一个count()

最大值(max):找出数学分数最高的学生姓名

// 使用for循环
int maxMathScore = Integer.MIN_VALUE;
String topMathStudent = "";
for (Student student : studentList) {
    if (student.getMathScore() > maxMathScore) {
        maxMathScore = student.getMathScore();
        topMathStudent = student.getName();
    }
}

// 使用Stream流
Optional<String> topMathStudent = studentList.stream()
    .max(Comparator.comparingInt(Student::getMathScore))
    .map(Student::getName);

for循环需要创建两个变量,并且要边循环边判断,逻辑需要自己写,stream流直接一个max()

分组(partitioningBy):学生的语文分数是否大于80分

// 使用for循环
Map<Boolean, List<Student>> partitionedMap = new HashMap<>();
List<Student> excellentStudents = new ArrayList<>();
List<Student> otherStudents = new ArrayList<>();

for (Student student : studentList) {
    if (student.getChineseScore() > 80) {
        excellentStudents.add(student);
    } else {
        otherStudents.add(student);
    }
}
partitionedMap.put(true, excellentStudents);
partitionedMap.put(false, otherStudents);

// 使用Stream流的partitioningBy
Map<Boolean, List<Student>> partitionedMapStream = studentList.stream()
        .collect(Collectors.partitioningBy(student -> student.getChineseScore() > 80));

这里就很明显了,stream流只用了两行就完成了分组筛选

总结

        在使用for循环和Stream流分别实现这三个逻辑时,我们可以总结出Stream流的一些优势:

  • 简洁性: Stream流提供了更为简洁的语法,减少了样板代码,使代码更易读。

  • 并行处理: Stream流天然支持并行处理,能够充分发挥多核处理器的性能,提高程序的运行效率。

  • 延迟执行: Stream流具有延迟执行的特性,只有在需要结果的时候才会执行计算,避免了不必要的性能开销。


 推荐阅读:

面试官:会用stream流筛选数据么?只会for循环?

我第一次使用AI写了一篇文章

Navicat激活(2024.01.13有效)

ArrayList(源码分析)—面试经典问题

SpringBoot-AOP深入浅出通俗易懂

最后我还整理汇总了⼀些 Java ⾯试相关的⾼质量 PDF 资料和免费Idea账号

公众号:Java小白,回复“⾯试” 和“idea破解”即可获取!

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

派大星的无情铁锤

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

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

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

打赏作者

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

抵扣说明:

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

余额充值