分組

自在现实發表於2024-06-25

轉載自:http://blog.csdn.net/u013078669/article/details/52717142

  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", "banana",  
                "apple", "orange", "banana", "papaya");  

Map<string long=""> result =  
        items.stream().collect(  
                Collectors.groupingBy(  
                        Function.identity(), Collectors.counting()  
                )  
        );  

System.out.println(result);  

}
/string>
輸出
[text] view plain copy
{
papaya=1, orange=1, banana=2, apple=3
}
1.2 分組, 計數和排序

[java] view plain copy
public static void main(String[] args) {

    //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()));  

    System.out.println(finalMap);  


}  


輸出:
[text] view plain copy
{
apple=3, banana=2, papaya=1, orange=1
}
2.使用者自定義物件集合分組, 計數、排序和求和

[java] view plain copy
public static void main(String[] args) {

   //3 apple, 2 banana, others 1  
   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"))  
   );  

   Map<string long=""> counting = items.stream().collect(  
           Collectors.groupingBy(Item::getName, Collectors.counting()));  

   System.out.println(counting);  

   Map<string integer=""> sum = items.stream().collect(  
           Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));  

   System.out.println(sum);  

}
lt;/string>
輸出
[text] view plain copy
//Group by + Count
{
papaya=1, banana=2, apple=3, orang=1, watermelon=1
}

//Group by + Sum qty
{
papaya=20, banana=30, apple=40, orang=10, watermelon=10
}
[java] view plain copy
public static void main(String[] args) {

    //3 apple, 2 banana, others 1  
    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"))  
            );  

    //group by price  
    Map<BigDecimal, List<item>> groupByPriceMap =  
        items.stream().collect(Collectors.groupingBy(Item::getPrice));  

    System.out.println(groupByPriceMap);  

    // group by price, uses 'mapping' to convert List<item> to Set<string>  
    Map<BigDecimal, Set<string>> result =  
            items.stream().collect(  
                    Collectors.groupingBy(Item::getPrice,  
                            Collectors.mapping(Item::getName, Collectors.toSet())  
                    )  
            );  

    System.out.println(result);  

}  


輸出
[text] view plain copy
{
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}
]
}

//group by + mapping to Set
{
19.99=[banana],
29.99=[orang, watermelon],
9.99=[papaya, apple]
}

相關文章