1.实体类分组
@Data
public class Student {
private String name;
private int age;
private int code;
public Student ( String name, int age, int code) {
this. name = name;
this. age = age;
this. code = code;
}
public static void main ( String[ ] args) {
Student student = new Student ( "小名" , 20 , 115 ) ;
Student student1 = new Student ( "小名" , 24 , 115 ) ;
Student student2 = new Student ( "小名" , 20 , 115 ) ;
List< Student> studentList = Arrays. asList ( student, student1, student2) ;
Map< Integer, List< Student>> ageMap = studentList. stream ( ) . collect ( Collectors. groupingBy ( Student: : getAge) ) ;
System. out. println ( "ageMap = " + ageMap) ;
Map< Integer, Long> ageMap2 = studentList. stream ( ) . collect ( Collectors. groupingBy ( Student: : getAge, Collectors. counting ( ) ) ) ;
System. out. println ( "ageMap2 = " + ageMap2) ;
}
}
2.Map分组
Map< String, List< Map< String, Object>> > xxxGroup = list. stream ( ) . collect ( groupingBy ( map -> map. get ( "xxx" ) . toString ( ) ) ) ;