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()));
});