1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| @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, List<Student>> ageMap = studentList.stream().collect(Collectors.groupingBy(e->e.getName+"-"+e.getAge));
Map<Integer, Long> ageMap2 = studentList.stream().collect(Collectors.groupingBy(Student::getAge, Collectors.counting())); System.out.println("ageMap2 = " + ageMap2); Map<Integer, Double> age2 = studentList.stream().collect(Collectors.groupingBy(Student::getAge, Collectors.averagingInteger(SysLog::getCode))); Map<Integer, Long> ageMap3 = studentList.stream().collect(Collectors.groupingBy(e->e.getName+"-"+e.getAge, Collectors.counting())); Map<String, Optional<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getGrade, Collectors.maxBy(Comparator.comparing(Student::getMark)))); Map<Double,Map<String,List<Student>>> map2 =students.stream().collect(Collectors.groupingBy(Student::getMark,Collectors.groupingBy(Student::getSubject))); Map<String,Map<String,Long>> map = students.stream().collect(Collectors.groupingBy(Student::getGrade, Collectors.groupingBy(student -> { if (student.getMark().intValue() >= 85){ return "合格"; }else{ return "不合格"; } },Collectors.counting()) ) ); } }
Map<String, List<EventDoc>> eventid2EventDocsMap = eventDocs.stream().sorted(Comparator.comparingInt(EventDoc::getOrderNum)).collect(Collectors.groupingBy(EventDoc::getEventId));
Map<Character, List<String>> groupingByList = Stream.of( "Monkey", "Lion", "Giraffe", "Lemur", "Lion" ) .collect(Collectors.groupingBy( s -> s.charAt(0) , ));
Map<String, String> username2AppcodesMap = sysUsernameAppcodes.stream().collect(Collectors.groupingBy(SysUsernameAppcode::getUserName, Collectors.mapping(SysUsernameAppcode::getAppCode, Collectors.joining(","))));
|