本篇內(nèi)容主要講解“java分組統(tǒng)計(jì)功能如何實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java分組統(tǒng)計(jì)功能如何實(shí)現(xiàn)”吧!
成都創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供棕樹數(shù)據(jù)中心 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
//下面是初始化的數(shù)據(jù) Listlist = new ArrayList (); Student student1 = new Student("李四1", "女", "一班"); Student student2 = new Student("李四2", "女", "一班"); Student student3 = new Student("李四3", "女", "一班"); Student student4 = new Student("李四4", "男", "一班"); Student student5 = new Student("李四5", "男", "一班"); Student student6 = new Student("李四6", "男", "二班"); Student student7 = new Student("李四7", "男", "二班"); Student student8 = new Student("李四8", "男", "二班"); Student student9 = new Student("李四9", "男", "二班"); list.add(student1); list.add(student2); list.add(student3); list.add(student4); list.add(student5); list.add(student6); list.add(student7); list.add(student8); list.add(student9);
在實(shí)際開發(fā)中合理的利用map自帶的方法,能解決很多問題
for (Student stu : list) { if (!map.containsKey(stu.getProvinceCode())) { ArrayListal = new ArrayList (); map.put(stu.getProvinceCode(), al.add(stu)); } else { map.get(stu.getProvinceCode()).add(stu); } }
MultimapmulMap = ArrayListMultimap.create(); for (Student stu : list) { mulMap.put(stu.getGrade,stu); }
畢竟java14都出來了,java8的新特性還是需要多了解
//一行就可以解決 Map> collect = list.stream().collect(Collectors.groupingBy(ArrearageDeal::getGrade));
上面三種當(dāng)時(shí)從代碼量上來看,java8的最簡潔。但是實(shí)際開發(fā)中結(jié)合具體場景來說2、3兩種都是不錯(cuò)的選擇。
// 分組統(tǒng)計(jì) MapcountMap = records.stream().collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting())); List countRecords = countMap.keySet().stream().map(key -> { String[] temp = key.split("_"); String productType = temp[0]; String country = temp[1]; Record record = new Record(); record.set("device_type", productType); record.set("location", country; record.set("count", countMap.get(key).intValue()); return record; }).collect(Collectors.toList());
到此,相信大家對(duì)“java分組統(tǒng)計(jì)功能如何實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!