java8stream的分組功能實(shí)例用法-創(chuàng)新互聯(lián)

這篇文章主要介紹“java8 stream的分組功能實(shí)例用法”,在日常操作中,相信很多人在java8 stream的分組功能實(shí)例用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java8 stream的分組功能實(shí)例用法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

站在用戶的角度思考問題,與客戶深入溝通,找到集賢網(wǎng)站設(shè)計(jì)與集賢網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋集賢地區(qū)。
public static void testStreamGroup(){  List<Student> stuList = new ArrayList<Student>();  Student stu1 = new Student("10001", "孫權(quán)", "1000101", 16, '男');  Student stu2 = new Student("10001", "曹操", "1000102", 16, '男');  Student stu3 = new Student("10002", "劉備", "1000201", 16, '男');  Student stu4 = new Student("10002", "大喬", "1000202", 16, '女');  Student stu5 = new Student("10002", "小喬", "1000203", 16, '女');  Student stu6 = new Student("10003", "諸葛亮", "1000301", 16, '男');  stuList.add(stu1);  stuList.add(stu2);  stuList.add(stu3);  stuList.add(stu4);  stuList.add(stu5);  stuList.add(stu6);  Map<String, List<Student>> collect = stuList.stream().collect(Collectors.groupingBy(Student::getClassId));  for(Map.Entry<String, List<Student>> stuMap:collect.entrySet()){     String classId = stuMap.getKey();     List<Student> studentList = stuMap.getValue();     System.out.println("classId:"+classId+",studentList:"+studentList.toString());  }}
classId:10002,studentList:[Student [classId=10002, name=劉備, studentId=1000201, age=16, sex=男], Student [classId=10002, name=大喬, studentId=1000202, age=16, sex=女], Student [classId=10002, name=小喬, studentId=1000203, age=16, sex=女]]classId:10001,studentList:[Student [classId=10001, name=孫權(quán), studentId=1000101, age=16, sex=男], Student [classId=10001, name=曹操, studentId=1000102, age=16, sex=男]]classId:10003,studentList:[Student [classId=10003, name=諸葛亮, studentId=1000301, age=16, sex=男]]

從上面的數(shù)據(jù)可以看出來,stuList被分成了三個組,每個組的key都是classId,而每個classId都對應(yīng)一個學(xué)生列表,這樣就很輕松地實(shí)現(xiàn)了數(shù)據(jù)的分離;此時(shí),無論需要對數(shù)據(jù)進(jìn)行怎樣的處理都會很容易。

到此,關(guān)于“java8 stream的分組功能實(shí)例用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

新聞名稱:java8stream的分組功能實(shí)例用法-創(chuàng)新互聯(lián)
URL分享:http://muchs.cn/article48/dppgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站制作ChatGPT、定制開發(fā)微信小程序、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司