Java8 stream groupingBy 多字段分組

public class Test {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();
        Student student = new Student("張三","女","11");
        Student student2 = new Student("張三","男","11");
        Student student3 = new Student("張三","男","11");
        Student student4 = new Student("李四","男","11");
        Student student5 = new Student("李四","男","11");
        Student student6 = new Student("王五","男","11");
        students.add(student);
        students.add(student2);
        students.add(student3);
        students.add(student4);
        students.add(student5);
        students.add(student6);
        Map<String,List<Student>> map = students.stream().collect(Collectors.groupingBy(Student :: getName));
        map.forEach((k,v) -> {
            System.out.println("k = "+ k + "||" +"v = " + v.stream().collect(Collectors.groupingBy(Student :: getSex)).size());
        });

    }
}

        Map<List<String>, List<Student>> map = students.stream()
                .collect(Collectors.groupingBy(f -> Arrays.asList(f.getName(),f.getSex())));

        map.forEach((o, o2) -> {
            List<Student> studentss = o2;
            studentss.forEach(s -> System.out.println(s.getName()));
        });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章