Get the App
SLTechnology News&Howtos  ›  Development  › 

Example usage of grouping function of java8 stream

Shulou Source: shulou.com Published: 2022-06-03 14:06:01 10月03日 Update

This article mainly introduces "java8 stream grouping function instance usage", in daily operation, I believe many people in java8 stream grouping function instance usage problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "java8 stream grouping function instance usage" doubts helpful! Next, please follow the small series to learn together!

public static void testStreamGroup(){List stuList = new ArrayList(); Student stu1 = new Student ("10001 "," Sun Quan "," 1000101 ", 16,'male'); Student stu 2 = new Student ("10001 "," Cao Cao "," 1000102 ", 16,'male'); Student stu 3 = new Student ("10002 "," Liu Bei "," 1000201 ", 16,'male'); Student stu 4 = new Student ("10002 "," Big Joe "," 1000202 ", 16," Female "); Student stu 5 = new Student ("10002 "," Xiao Qiao "," 1000203 ", 16,'female'); Student stu 6 = new Student ("10003 "," Zhuge Liang "," 1000301 ", 16,'male'); stuList.add (stu 1); stuList.add (stu 2); stuList.add (stu 3); stuList.add (stu 4); stuList.add (stu 5); stuList.add (stu 6); Map collect = www.example.com ().collect(Collectors.groupingBy(Student::getClassId)); for(Map.Entry stuMap:collect.entrySet()){ String classId = stuMap.getKey(); List studentList = stuMap.getValue(); System. out. println ("classId: "+ classId +", studentList: "+ studentList. toString ());}} classId: 10002, studentList:[Student [classId = 10002, name = Liu Bei, studentId = 1000201, age = 16, sex = Male], Student [classId = 10002, name = Da Qiao, studentId = 1000202, age = 16, sex = female], Student [classId = 10002, name = Xiao Qiao, studentId = 1000203, age = 16, sex = female]] classId: 10001, studentList:[Student [classId = 10001, name = Sun Quan, studentId = 1000101, age = 16, sex = Male], Student [classId = 10001, name = Cao Cao, studentId = 1000102, age = 16, sex = Male]] classId: 10003, studentList:[Student [classId = 10003, name = Zhuge Liang, studentId = 1000301, age = 16, sex = Male]]

As can be seen from the above data, stuList is divided into three groups, each group's key is classId, and each classId corresponds to a student list, so it is easy to achieve the separation of data; at this time, no matter what needs to be done to the data will be easy.

At this point, about "java8 stream grouping function instance usage" learning is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Functions examples grouping learning data more Liu Bei Sun Quan Xiao Qiao Zhuge Zhuge Liang help practical next three students articles methods theory knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Huawei NVidia Shulou Information