JAVA 8按某些属性分组
·
JAVA 8按某些属性分组
例如下面的先按名字再按地方进行分组
class Student {
String stud_id;
String stud_name;
String stud_location;
public String getStud_id() {
return stud_id;
}
public String getStud_name() {
return stud_name;
}
public String getStud_location() {
return stud_location;
}
Student(String sid, String sname, String slocation) {
this.stud_id = sid;
this.stud_name = sname;
this.stud_location = slocation;
}
}
class Temp
{
public static void main(String args[])
{
Stream<Student> studs =
Stream.of(new Student("1726", "小红", "上海"),
new Student("4321", "小米", "北京"),
new Student("2234", "小明", "深圳"),
new Student("7765", "小胡", "北京"),
new Student("1726", "小胡", "上海"));
Map<String, Map<Object, List<Student>>> map= studs.collect(Collectors.groupingBy(Student::getStud_name,Collectors.groupingBy(Student::getStud_location)));
System.out.println(map);//print by name and then location
}
}
结果
更多推荐



所有评论(0)