Streaming exercises in java8

Posted May 25, 20202 min read

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,   ,   \]

Related Posts