溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java中分組統(tǒng)計(jì)的實(shí)現(xiàn)方法有哪些

發(fā)布時(shí)間:2021-07-06 17:52:18 來源:億速云 閱讀:277 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“java中分組統(tǒng)計(jì)的實(shí)現(xiàn)方法有哪些”,在日常操作中,相信很多人在java中分組統(tǒng)計(jì)的實(shí)現(xiàn)方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”java中分組統(tǒng)計(jì)的實(shí)現(xiàn)方法有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

平時(shí)工作中,很多時(shí)候都會(huì)用到對(duì)數(shù)據(jù)進(jìn)行分組操作,例如一個(gè)學(xué)生對(duì)象,有班級(jí)、名字、性別、分?jǐn)?shù)等,需要按班級(jí)分組統(tǒng)計(jì),該怎么操作呢?一個(gè)合理的算法可以提升不少效率。

大家看一下下面的案例:

//下面是初始化的數(shù)據(jù)
       List<Student> list = new ArrayList<Student>();
        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);

1.合理利用map操作

在實(shí)際開發(fā)中合理的利用map自帶的方法,能解決很多問題

 for (Student  stu : list) {
            if (!map.containsKey(stu.getProvinceCode())) {
                ArrayList<ArrearageDeal> al = new ArrayList<ArrearageDeal>();
                map.put(stu.getProvinceCode(),  al.add(stu));
            } else {
                map.get(stu.getProvinceCode()).add(stu);
            }
        }

2.利用guava的Multimap

Multimap<String, Student> mulMap = ArrayListMultimap.create();
for (Student  stu : list) {
       mulMap.put(stu.getGrade,stu);   
}

3.使用jdk8新特性–不要排斥新東西

畢竟java14都出來了,java8的新特性還是需要多了解

//一行就可以解決
Map<String, List<Student  >> collect = list.stream().collect(Collectors.groupingBy(ArrearageDeal::getGrade));

上面三種當(dāng)時(shí)從代碼量上來看,java8的最簡(jiǎn)潔。但是實(shí)際開發(fā)中結(jié)合具體場(chǎng)景來說2、3兩種都是不錯(cuò)的選擇。

Java8 多個(gè)字段分組統(tǒng)計(jì)

// 分組統(tǒng)計(jì)
Map<String, Long> countMap = records.stream().collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting()));
 List<Record> 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());

到此,關(guān)于“java中分組統(tǒng)計(jì)的實(shí)現(xiàn)方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI