码迷,mamicode.com
首页 > 其他好文 > 详细

Stream流的使用

时间:2018-12-16 23:31:52      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:super   out   col   void   group   etag   int   student   name   

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]}

 

Stream流的使用

标签:super   out   col   void   group   etag   int   student   name   

原文地址:https://www.cnblogs.com/liuxuelin/p/10128044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!