行为抽象和Lambda分组

举报
xcc-2022 发表于 2022/07/22 14:48:38 2022/07/22
【摘要】 1. 分组, 计数和排序1.1 分组, 计数[java]  view plain  copy   public static void main(String[] args) {        //3 apple, 2 banana, others 1      List<string> items =              Arrays.asList("apple", "apple",...

1. 分组, 计数和排序

1.1 分组, 计数

[java]  view plain  copy

  1.    public static void main(String[] args) {  
  2.   
  3.     //3 apple, 2 banana, others 1  
  4.     List<string> items =  
  5.             Arrays.asList("apple""apple""banana",  
  6.                     "apple""orange""banana""papaya");  
  7.   
  8.     Map<string long=""> result =  
  9.             items.stream().collect(  
  10.                     Collectors.groupingBy(  
  11.                             Function.identity(), Collectors.counting()  
  12.                     )  
  13.             );  
  14.   
  15.     System.out.println(result);  
  16.   
  17.   
  18. }  
  19. /string></string>  

输出

[text]  view plain  copy

  1. {  
  2.     papaya=1, orange=1, banana=2, apple=3  
  3. }  

1.2 分组, 计数和排序

[java]  view plain  copy

  1.  public static void main(String[] args) {  
  2.   
  3.         //3 apple, 2 banana, others 1  
  4.         List<string> items =  
  5.                 Arrays.asList("apple""apple""banana",  
  6.                         "apple""orange""banana""papaya");  
  7.   
  8.         Map<string long=""> result =  
  9.                 items.stream().collect(  
  10.                         Collectors.groupingBy(  
  11.                                 Function.identity(), Collectors.counting()  
  12.                         )  
  13.                 );  
  14.   
  15.         Map<string long=""> finalMap = new LinkedHashMap<>();  
  16.   
  17.         //Sort a map and add to finalMap  
  18.         result.entrySet().stream()  
  19.                 .sorted(Map.Entry.<string long="">comparingByValue()  
  20.                         .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));  
  21.   
  22.         System.out.println(finalMap);  
  23.   
  24.   
  25.     }  
  26. </string></string></string></string>  

输出:

[text]  view plain  copy

  1. {  
  2.     apple=3, banana=2, papaya=1, orange=1  
  3. }  

2.用户自定义对象集合分组, 计数、排序和求和

[java]  view plain  copy

  1. public static void main(String[] args) {  
  2.   
  3.        //3 apple, 2 banana, others 1  
  4.        List<item> items = Arrays.asList(  
  5.                new Item("apple"10new BigDecimal("9.99")),  
  6.                new Item("banana"20new BigDecimal("19.99")),  
  7.                new Item("orang"10new BigDecimal("29.99")),  
  8.                new Item("watermelon"10new BigDecimal("29.99")),  
  9.                new Item("papaya"20new BigDecimal("9.99")),  
  10.                new Item("apple"10new BigDecimal("9.99")),  
  11.                new Item("banana"10new BigDecimal("19.99")),  
  12.                new Item("apple"20new BigDecimal("9.99"))  
  13.        );  
  14.   
  15.        Map<string long=""> counting = items.stream().collect(  
  16.                Collectors.groupingBy(Item::getName, Collectors.counting()));  
  17.   
  18.        System.out.println(counting);  
  19.   
  20.        Map<string integer=""> sum = items.stream().collect(  
  21.                Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));  
  22.   
  23.        System.out.println(sum);  
  24.   
  25.    }  
  26. lt;/string></string></item>  

输出

[text]  view plain  copy

  1. //Group by + Count  
  2. {  
  3.     papaya=1, banana=2, apple=3, orang=1, watermelon=1  
  4. }  
  5.   
  6. //Group by + Sum qty  
  7. {  
  8.     papaya=20, banana=30, apple=40, orang=10, watermelon=10  
  9. }  
[java]  view plain  copy

  1. public static void main(String[] args) {  
  2.   
  3.         //3 apple, 2 banana, others 1  
  4.         List<item> items = Arrays.asList(  
  5.                 new Item("apple"10new BigDecimal("9.99")),  
  6.                 new Item("banana"20new BigDecimal("19.99")),  
  7.                 new Item("orang"10new BigDecimal("29.99")),  
  8.                 new Item("watermelon"10new BigDecimal("29.99")),  
  9.                 new Item("papaya"20new BigDecimal("9.99")),  
  10.                 new Item("apple"10new BigDecimal("9.99")),  
  11.                 new Item("banana"10new BigDecimal("19.99")),  
  12.                 new Item("apple"20new BigDecimal("9.99"))  
  13.                 );  
  14.   
  15.         //group by price  
  16.         Map<BigDecimal, List<item>> groupByPriceMap =  
  17.             items.stream().collect(Collectors.groupingBy(Item::getPrice));  
  18.   
  19.         System.out.println(groupByPriceMap);  
  20.   
  21.         // group by price, uses 'mapping' to convert List<item> to Set<string>  
  22.         Map<BigDecimal, Set<string>> result =  
  23.                 items.stream().collect(  
  24.                         Collectors.groupingBy(Item::getPrice,  
  25.                                 Collectors.mapping(Item::getName, Collectors.toSet())  
  26.                         )  
  27.                 );  
  28.   
  29.         System.out.println(result);  
  30.   
  31.     }  
  32. </string></string></item></item></item>  

输出

[text]  view plain  copy

  1. {  
  2.     19.99=[  
  3.             Item{name='banana', qty=20, price=19.99},   
  4.             Item{name='banana', qty=10, price=19.99}  
  5.         ],   
  6.     29.99=[  
  7.             Item{name='orang', qty=10, price=29.99},   
  8.             Item{name='watermelon', qty=10, price=29.99}  
  9.         ],   
  10.     9.99=[  
  11.             Item{name='apple', qty=10, price=9.99},   
  12.             Item{name='papaya', qty=20, price=9.99},   
  13.             Item{name='apple', qty=10, price=9.99},   
  14.             Item{name='apple', qty=20, price=9.99}  
  15.         ]  
  16. }  
  17.   
  18. //group by + mapping to Set  
  19. {  
  20.     19.99=[banana],   
  21.     29.99=[orang, watermelon],   
  22.     9.99=[papaya, apple]  
  23. }  
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。