国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Stream流實(shí)踐(五):使用group by然后緊跟sum sort等操作

這篇具有很好參考價(jià)值的文章主要介紹了Stream流實(shí)踐(五):使用group by然后緊跟sum sort等操作。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

前言

本文會用幾個(gè)例子去講解Stream流 group by基本用法,以及group by分組之后對于分組數(shù)據(jù)的匯總、排序等操作文章來源地址http://www.zghlxwxcb.cn/news/detail-620807.html

1、 Group By 計(jì)算+ 匯總

1.1 Group by 集合,并展示最后的匯總數(shù)據(jù)


 List<String> items =
                Arrays.asList("apple", "apple", "banana",
                        "apple", "orange", "banana", "papaya");

        Map<String, Long> result =
                items.stream().collect(
                        Collectors.groupingBy(
                        //Function.identity()-》指向本身
                        //Collectors.counting() 匯總個(gè)數(shù)
                                Function.identity(), Collectors.counting()
                        )
                );

//結(jié)果
papaya=1, orange=1, banana=2, apple=3      

1.2 Group by 集合,并且將他按順序加入到新Map中去


        //3 apple, 2 banana, others 1
        List<String> items =
                Arrays.asList("apple", "apple", "banana",
                        "apple", "orange", "banana", "papaya");

        Map<String, Long> result =
                items.stream().collect(
                        Collectors.groupingBy(
                                Function.identity(), Collectors.counting()
                        )
                );

        Map<String, Long> finalMap = new LinkedHashMap<>();

        //Sort a map and add to finalMap
        result.entrySet().stream()
                .sorted(Map.Entry.<String, Long>comparingByValue()
                        .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));

//結(jié)果
apple=3, banana=2, papaya=1, orange=1

2、List 集合

2.1 對象的基本處理

public class Item {

    private String name;
    private int qty;
    private BigDecimal price;
}

 List<Item> items = Arrays.asList(
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 20, new BigDecimal("19.99")),
                new Item("orang", 10, new BigDecimal("29.99")),
                new Item("watermelon", 10, new BigDecimal("29.99")),
                new Item("papaya", 20, new BigDecimal("9.99")),
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 10, new BigDecimal("19.99")),
                new Item("apple", 20, new BigDecimal("9.99"))
        );

//先針對name屬性分組,然后計(jì)算總數(shù)
        Map<String, Long> counting = items.stream().collect(
                Collectors.groupingBy(Item::getName, Collectors.counting()));

        System.out.println(counting);

//先針對name屬性分組,然后根據(jù)Qty屬性進(jìn)行求和
        Map<String, Integer> sum = items.stream().collect(
                Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));

        System.out.println(sum);
//結(jié)果
{
	papaya=1, banana=2, apple=3, orang=1, watermelon=1
}

//Group by + Sum qty
{
	papaya=20, banana=30, apple=40, orang=10, watermelon=10
}

2.2 Collectors.mapping 的例子

    List<Item> items = Arrays.asList(
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 20, new BigDecimal("19.99")),
                new Item("orang", 10, new BigDecimal("29.99")),
                new Item("watermelon", 10, new BigDecimal("29.99")),
                new Item("papaya", 20, new BigDecimal("9.99")),
                new Item("apple", 10, new BigDecimal("9.99")),
                new Item("banana", 10, new BigDecimal("19.99")),
                new Item("apple", 20, new BigDecimal("9.99"))
                );

		//根據(jù)price分組
        Map<BigDecimal, List<Item>> groupByPriceMap = 
			items.stream().collect(Collectors.groupingBy(Item::getPrice));

        System.out.println(groupByPriceMap);

		//先根據(jù)price分組 然后再將分好組的數(shù)據(jù),通過Collectors.mapping再一次把name放入set中(mappinng 使得 List -> set)
        Map<BigDecimal, Set<String>> result =
                items.stream().collect(
                        Collectors.groupingBy(Item::getPrice,
                                Collectors.mapping(Item::getName, Collectors.toSet())
                        )
                );

        System.out.println(result);
//結(jié)果
        	19.99=[
			Item{name='banana', qty=20, price=19.99}, 
			Item{name='banana', qty=10, price=19.99}
		], 
			29.99=[
			Item{name='orang', qty=10, price=29.99}, 
			Item{name='watermelon', qty=10, price=29.99}
		], 
			9.99=[
			Item{name='apple', qty=10, price=9.99}, 
			Item{name='papaya', qty=20, price=9.99}, 
			Item{name='apple', qty=10, price=9.99}, 
			Item{name='apple', qty=20, price=9.99}
		]
}

//通過mapping轉(zhuǎn)變?yōu)閟et
{
	19.99=[banana], 
	29.99=[orang, watermelon], 
	9.99=[papaya, apple]
}

到了這里,關(guān)于Stream流實(shí)踐(五):使用group by然后緊跟sum sort等操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ES多個(gè)字段group by操作

    ES多個(gè)字段group by操作

    以下操作基于es6.8 這種方式查詢出來的數(shù)據(jù)不是扁平化的,而是一層套一層的,比如字段一套字段二。 結(jié)果,one下面的buckets里面是two,每個(gè)two下面有自己的bukets,就是two的值和count。 封裝一個(gè)通用的聚合查詢并映射到j(luò)ava類中 ? 這種方式查出來的數(shù)據(jù)更扁平化,容易被接受

    2024年02月15日
    瀏覽(20)
  • 使用Java的stream().sorted方法對集合進(jìn)行排序

    Java Stream API 提供了豐富的方法來對流中的元素進(jìn)行處理和操作。其中, sorted() 方法用于對流中的元素進(jìn)行排序。本文將深入探討 sorted() 方法的用法、示例代碼以及詳細(xì)解釋,以幫助您更好地理解和使用這個(gè)方法。 StreamT sorted() : 這個(gè)方法用于對流中的元素進(jìn)行自然排序。要

    2024年02月04日
    瀏覽(20)
  • Java8-使用stream.sorted()對List排序

    1.流的定義 Stream 中文稱為 “流”,通過將集合轉(zhuǎn)換為這么一種叫做 “流” 的元素序列,通過聲明性方式,能夠?qū)现械拿總€(gè)元素進(jìn)行一系列并行或串行的操作! 如果流中的元素的類實(shí)現(xiàn)了 Comparable 接口,即有自己的排序規(guī)則,那么可以直接調(diào)用 sorted() 方法對元素進(jìn)行排

    2024年02月16日
    瀏覽(93)
  • Mysql group by使用示例

    Mysql group by使用示例

    總數(shù)據(jù): 索引情況:

    2024年02月11日
    瀏覽(25)
  • GROUP BY 使用方法詳解

    GROUP BY 使用方法詳解

    group by是開發(fā)中經(jīng)常用到的SQL語句,從字面意思來看就是根據(jù)哪個(gè)字段或者哪幾個(gè)字段對查詢到的數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì),既然是分組統(tǒng)計(jì)那如何分組呢?所以group by通常都是和聚合函數(shù)還有having一起使用。 select 聚合函數(shù)(字段1),字段2 from 表名 where 條件 group by 字段2,字段3 或者

    2024年02月13日
    瀏覽(16)
  • sql中g(shù)roup by 的使用

    sql中g(shù)roup by 的使用

    1、概述 Group By 從字面意義上理解就是根據(jù)By指定的規(guī)則對數(shù)據(jù)進(jìn)行分組,所謂的分組就是將一個(gè)數(shù)據(jù)集劃分為若干個(gè)小區(qū)域,然后針對若干個(gè)小區(qū)域進(jìn)行數(shù)據(jù)處理 2、原始表 ?3、簡單的Group By 示例1 select 類別,數(shù)量 as 數(shù)量之和 from A group by 類別 返回結(jié)果如下表,實(shí)際上就是

    2024年02月16日
    瀏覽(30)
  • mysql中的group by 和 having使用

    mysql中的group by 和 having使用

    mysql中的group by 和 having 使用 –sql中的group by 用法解析: – Group By語句從英文的字面意義上理解就是“根據(jù)(by)一定的規(guī)則進(jìn)行分組(Group)”。 –它的作用是通過一定的規(guī)則將一個(gè)數(shù)據(jù)集劃分成若干個(gè)小的區(qū)域,然后針對若干個(gè)小區(qū)域進(jìn)行數(shù)據(jù)處理。 –注意:group by 是先排序后

    2024年02月08日
    瀏覽(22)
  • 如何使用SQL系列 之 如何在SQL中使用GROUP BY和ORDER BY

    結(jié)構(gòu)化查詢語言(SQL)數(shù)據(jù)庫可以跨多個(gè)表存儲和管理大量數(shù)據(jù)。對于大型數(shù)據(jù)集,理解如何排序數(shù)據(jù)是很重要的,特別是對于分析結(jié)果集或?yàn)閳?bào)告或外部通信組織數(shù)據(jù)。 SQL中有兩個(gè)常用的用于數(shù)據(jù)排序的語句: GROUP BY 和 ORDER BY 。 GROUP BY 語句根據(jù)查詢中指定的列對數(shù)據(jù)進(jìn)行分組

    2024年02月09日
    瀏覽(20)
  • 【flink番外篇】9、Flink Table API 支持的操作示例(6)- 表的聚合(group by、Distinct、GroupBy/Over Window Aggregation)操作

    一、Flink 專欄 Flink 專欄系統(tǒng)介紹某一知識點(diǎn),并輔以具體的示例進(jìn)行說明。 1、Flink 部署系列 本部分介紹Flink的部署、配置相關(guān)基礎(chǔ)內(nèi)容。 2、Flink基礎(chǔ)系列 本部分介紹Flink 的基礎(chǔ)部分,比如術(shù)語、架構(gòu)、編程模型、編程指南、基本的datastream api用法、四大基石等內(nèi)容。 3、

    2024年02月02日
    瀏覽(25)
  • LeetCode //167. Two Sum II - Input Array Is Sorted

    Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 = index1 index2 numbers.length. Return the indices of the two numbers, index1 and index2 , added by one as an integer array [index1

    2024年02月15日
    瀏覽(28)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包