groupby统计数量
import java.util.Objects;
public class Student {
/**
* 学号
*/
private long id;
private String name;
private int age;
/**
* 年级
*/
private int grade;
/**
* 专业
*/
private String major;
/**
* 学校
*/
private String school;
public Student() {
}
public Student(long id, String name, int age, int grade, String major, String school) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
this.major = major;
this.school = school;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(school, student.school);
}
@Override
public int hashCode() {
return Objects.hash(school);
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
", major='" + major + '\'' +
", school='" + school + '\'' +
'}';
}
}
@Test
public void FileTest1() throws IOException {
List<Student> students = new ArrayList<Student>() {
{
add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学"));
add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学"));
add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学"));
add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学"));
add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学"));
add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学"));
add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学"));
add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学"));
add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学"));
}
};
Map<String, Long> group2 = students.stream().collect(
Collectors.groupingBy(Student::getSchool,
Collectors.counting()));
System.out.println(group2);
}
{南京大学=1, 武汉大学=3, 浙江大学=2, 华中科技大学=3}
Process finished with exit code 0