Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example usage of grouping function of java8 stream

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report