集合stream流过滤,排序,分组,求和等流式案例

1.8之后引入了stream流之后,java对集合的操作变得更加丰富起来,下面列举了一下非常常用的流式操作,供大家参考

代码

/**
 * 存储对象
 *
 * @date 2021/12/22
 */
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    private int id;

    private String name;

    private int age;

    private String sex;

    public Student() {
    }

    public Student(int id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
public class ListExample {

    public static void main(String[] args) {

        // 数据准备
        Student student1 = new Student(1, "张三", 18, "男");
        Student student2 = new Student(2, "李四", 19, "女");
        Student student3 = new Student(3, "王五", 17, "女");
        Student student4 = new Student(4, "赵六", 18, "男");
        Student student5 = new Student(5, "张三", 20, "男");
        Student student6 = new Student(6, "jack", 21, "男");
        Student student7 = new Student(7, "rose", 20, "女");
        Student student8 = new Student(8, "tom", 18, "男");
        Student student9 = new Student(9, "", 18, "男");

        // 批量往集合中添加元素
        List<Student> students = new ArrayList<>();
        Collections.addAll(students, student1, student2, student3, student4, student5, student6, student7, student8, student9);
        System.out.println("原学生集合 = " + students);

        // 筛选出姓名为张三的学生
        List<Student> list1 =
                students.stream().filter(o -> Objects.equals(o.getName(), "张三"))
                        .collect(Collectors.toList());
        System.out.println("姓名等于张三的学生集合 = " + list1);

        // 筛选出姓名不为张三的学生
        List<Student> list2 =
                students.stream().filter(o -> !Objects.equals(o.getName(), "张三"))
                        .collect(Collectors.toList());
        System.out.println("姓名不等于张三的学生集合 = " + list2);

        // 晒选出年龄大于20的学生
        List<Student> list3 = students.stream().filter(o -> o.getAge() > 20)
                .collect(Collectors.toList());
        System.out.println("年龄大于20的学生的集合 = " + list3);

        // 统计出男生多少人,女生多少人
        long manNumber = students.stream().filter(o -> Objects.equals(o.getSex(), "男")).count();
        long womanNumber = students.stream().filter(o -> Objects.equals(o.getSex(), "女")).count();
        System.out.println("男学生的人数 = " + manNumber);
        System.out.println("女学生的人数 = " + womanNumber);

        // 判断学生中是否有年龄大于25的学生
        // anyMatch 若是有一个符合条件,返回true
        boolean b = students.stream().anyMatch(o -> o.getAge() > 25);
        String str = b ? "含有" : "不含有";
        System.out.println("是否含有年龄大于25的学生 : " + str);

        // 拿到所有学生的姓名集合
        List<String> collect =
                students.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println("所有学生的姓名集合 = " + collect);

        // 拿到所有学生的姓名集合,过滤掉重复,为空的姓名
        List<String> list5 =
                students.stream().map(Student::getName)
                        .distinct()
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());
        System.out.println("所有不重复学生的姓名集合 = " + list5);

        // 按照年龄降序,姓名升序后的学生信息
        List<Student> list6 = students.stream()
                .sorted(Comparator.comparing(Student::getAge).reversed()
                        .thenComparing(Student::getName)).collect(Collectors.toList());
        System.out.println("按照年龄降序,姓名升序排列后的学生信息 = " + list6);

        // list->map,按照id分组, 此时id无重复,map共有9个学生信息
        Map<Integer, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getId));
        System.out.println("按照id分组后的数据 = " + map);

        // list->map,按照年龄分组,一共有5个年龄段,分别是17,18,19,20,21,所以map1的size为5
        Map<Integer, List<Student>> map1 = students.stream().collect(Collectors.groupingBy(Student::getAge));
        System.out.println("按照年龄分组后的数据 = " + map1);

        // list->map key-value一一对应,此时一个map对应一个学生,key为id,value为学生信息,注意此时id不能重复,否则会有重复的key报错
        Map<Integer, Student> idMap = students.stream().collect(Collectors.toMap(Student::getId, o -> o));
        System.out.println("id对应的学生map = " + idMap);

    }

}

总结

流式编程引入了,代码变的很简洁,希望能帮助到大家

上一篇:Redis Stream 的一点理解


下一篇:【规范代码】- JAVA 8