前言
本文會用幾個(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]
}
文章來源:http://www.zghlxwxcb.cn/news/detail-620807.html
到了這里,關(guān)于Stream流實(shí)踐(五):使用group by然后緊跟sum sort等操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!