0%

java8-stream流练习1

1. 代码示例

2. filter/map/collect/count

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author weijun.nie
 * @Date 2020/5/25 17:37
 * @Version 1.0
 */
@Data
@Slf4j
public class Student {
    // 姓名
    private String name;
    // 年龄
    private int age;
    // 性别
    private Gender gender;
    // 小区
    private Community community;

    public Student(String name, int age, Gender gender, Community community) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.community = community;
    }

    @Data
    static class Community {
        private String address;
        private String name;
        private int count; // 小区人数

        public Community(String address, String name, int count) {
            this.address = address;
            this.name = name;
            this.count = count;
        }
    }

    public enum Gender {
        MALE,
        FEMALE;
    }

    public static void main(String[] args) {
        Community hdb = new Community("黄渠头路", "海德堡", 2000);
        Community yglz = new Community("黄渠头路", "阳光丽兹", 1500);
        Community tdzy = new Community("黄渠头路", "唐顿庄园", 1000);
        Community qjld = new Community("雁翔路", "曲江龙邸", 1000);
        List<Student> students = Arrays.asList(
                new Student("小黑", 5, Gender.MALE, hdb),
                new Student("小白", 6, Gender.FEMALE, hdb),
                new Student("小红", 7, Gender.FEMALE, yglz),
                new Student("小蓝", 5, Gender.MALE, tdzy),
                new Student("小陈", 3, Gender.FEMALE, hdb),
                new Student("小张", 3, Gender.MALE, yglz),
                new Student("小吴", 5, Gender.MALE, hdb),
                new Student("小刘", 12, Gender.FEMALE, hdb),
                new Student("大牛", 16, Gender.MALE, qjld),
                new Student("大李", 15, Gender.MALE, hdb),
                new Student("大Q", 42, Gender.FEMALE, qjld),
                new Student("大E", 35, Gender.MALE, hdb)
        );

        // 1. 汇总小区名为"海德堡"的学生的总数;
        long count = students.stream().filter(s -> hdb == s.getCommunity()).count();
        log.info("汇总小区名为 海德堡 的学生的总数:{}", count);
        // 2. 汇总小区名为"海德堡"的学生的姓名集合;
        List<String> hdbStudentNames = students.stream().filter(s -> s.getCommunity() == hdb).map(s -> s.getName()).collect(Collectors.toList());
        log.info("汇总小区名为 海德堡 的学生的姓名集合", hdbStudentNames);
        // 3. 返回住在阳光丽兹+海德堡的学生的平均年龄;
        Double collect = students.stream().filter(s -> s.getCommunity() == tdzy || s.getCommunity() == yglz).collect(Collectors.averagingInt(Student::getAge));
        log.info("住在阳光丽兹+海德堡的学生的平均年龄为:{}岁", collect);
        // 4. 拿到年龄大于平均年龄的所有学生的姓名集合;
        Double avgAge = students.stream().collect(Collectors.averagingInt(Student::getAge));
        Set set = students.stream().filter(s -> s.getAge() > avgAge).map(Student::getName).collect(Collectors.toCollection(HashSet::new));
        log.info("所有学生平均年龄:{}, 大于平均年龄的学生有:{}", avgAge, set);
    }
}

汇总小区名为 海德堡 的学生的总数:7
汇总小区名为 海德堡 的学生的姓名集合
住在阳光丽兹+海德堡的学生的平均年龄为:5.0岁
所有学生平均年龄:12.833333333333334, 大于平均年龄的学生有:[大Q, 大E, 大李, 大牛]