Java 8 – List分组GroupBy

来源:互联网 发布:java手机游戏下载 编辑:程序博客网 时间:2024/05/16 04:33

转载自:http://blog.csdn.net/u013078669/article/details/52717142

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. }  
0 0
原创粉丝点击