我想用java收集数据(“男性”)。 但是,我无法理解这个过程。 为什么要使用分类器?在这段代码中?
Collector<Student,?,Map<Student.Gender,List<student>>> collector = Collectors.groupingBy(classifier);
public class GroupingBYExample{
public static void main(String[] args){
List<Student> totalList = new ArrayList<>();
totalList.add(new Student("Nafla",100,Student.Gender.Male);
totalList.add(new Student("Jacki",99,Student.Gender.FEMale);
Stream<Student> totalStream= totalList.stream();
Function<Student,Student.Gender> classifier = Student:: getGender;
Collector<Student,?,Map<Student.Gender,List<student>>> collector = Collectors.groupingBy(classifier);
Map<Student.Gender,List<Student>> mapByGender = totalStream.collect(collector);
System.out.println("Male Student:");
mapByGender.get(Student.Gender.MALE).stream().forEach(s->System.out.println(s.getName()+" "));
}
}
public class Student{
public enum Gender{MALE,FEMALE}
private String name;
private int score;
private Gedner gender;
private City city;
Student(String name,int score,Gender gender){
this.name=name;
this.score=score;
this.gender=gender;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getScore() {return score;}
public void setScore(int score) {this.score = score;}
public Gender getGender() {return gender;}
public void setGender(Gender gender) {this.gender = gender;}
}