Stream流的使用

1.定义实体类Student:

复制代码
public class Student {

    private int age;
    private String name;
    private int score;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public Student(int age, String name, int score) {
        super();
        this.age = age;
        this.name = name;
        this.score = score;
    }

}
复制代码

2.创建Student集合

Student s1 = new Student(16, "aa", 44);
Student s2 = new Student(17, "bb", 88);
Student s3 = new Student(17, "cc", 99);
Student s4 = new Student(19, "dd", 66);

List<Student> students = Arrays.asList(s1, s2, s3, s4);

3.采用Stream流过滤得到分数大于60的学生并按年龄 分组,分组结果封装到Map集合

ConcurrentMap<Integer, List<Student>> collect = students.stream().filter(s->{
            return s.getScore()>60;
        }).collect(Collectors.groupingByConcurrent(Student::getAge,Collectors.toList()));
        
        System.out.println(collect);

运行结果:

  {17=[com.liuxuelin.Student@7b23ec81, com.liuxuelin.Student@6acbcfc0], 19=[com.liuxuelin.Student@5f184fc6]}

 

posted @   foreast  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示