}
246
}
1 /**
2 * 分组List并显示其总数
3 */
4 @Test
5 void test1(){
6 //3 apple, 2 banana, others 1
7 List items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya");
8 Map<String, Long> collect = items.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
9 System.out.println(collect); //{papaya=1, orange=1, banana=2, apple=3}
10 }
12 /**
13 * 添加排序
14 */
15 @Test
16 void test2(){
17 List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya");
18 Map<String, Long> map = items.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
19 Map<String, Long> finalMap = new LinkedHashMap<>();
20 map.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByValue().reversed()).forEachOrdered(e->finalMap.put(e.getKey(),e.getValue()));
22 System.out.println(finalMap); //{apple=3, banana=2, papaya=1, orange=1}
23 }
25 /**
26 * 按姓名+ 数字或数量组合
27 */
28 @Test
29 void test3(){
30 //3 apple, 2 banana, others 1
31 List<Item> items = Arrays.asList(
32 new Item("apple", 10, new BigDecimal("9.99")),
33 new Item("banana", 20, new BigDecimal("19.99")),
34 new Item("orange", 10, new BigDecimal("29.99")),
35 new Item("watermelon", 10, new BigDecimal("29.99")),
36 new Item("papaya", 20, new BigDecimal("9.99")),
37 new Item("apple", 10, new BigDecimal("9.99")),
38 new Item("banana", 10, new BigDecimal("19.99")),
39 new Item("apple", 20, new BigDecimal("9.99"))
40 );
41 Map<String, Long> counting = items.stream().collect(Collectors.groupingBy(Item::getName, Collectors.counting()));
42 System.out.println(counting); //{papaya=1, orange=1, banana=2, apple=3, watermelon=1}
44 Map<String, Integer> map = items.stream().collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getNumber)));
45 System.out.println(map); //{papaya=20, orange=10, banana=30, apple=40, watermelon=10}
46 }
48 /**
49 * 按价格分组
50 */
51 @Test
52 void test4(){
53 //3 apple, 2 banana, others 1
54 List<Item> items = Arrays.asList(
55 new Item("apple", 10, new BigDecimal("9.99")),
56 new Item("banana", 20, new BigDecimal("19.99")),
57 new Item("orange", 10, new BigDecimal("29.99")),
58 new Item("watermelon", 10, new BigDecimal("29.99")),
59 new Item("papaya", 20, new BigDecimal("9.99")),
60 new Item("apple", 10, new BigDecimal("9.99")),
61 new Item("banana", 10, new BigDecimal("19.99")),
62 new Item("apple", 20, new BigDecimal("9.99"))
63 );
64 Map<BigDecimal, List<Item>> decimalListMap = items.stream().collect(Collectors.groupingBy(Item::getPrice));
65 System.out.println(decimalListMap);
66 //{19.99=[Item(name=banana, number=20, price=19.99),
67 // Item(name=banana, number=10, price=19.99)],
68 // 29.99=[Item(name=orange, number=10, price=29.99),
69 // Item(name=watermelon, number=10, price=29.99)],
70 // 9.99=[Item(name=apple, number=10, price=9.99),
71 // Item(name=papaya, number=20, price=9.99),
72 // Item(name=apple, number=10, price=9.99),
73 // Item(name=apple, number=20, price=9.99)]}
75 Map<BigDecimal, Set<String>> setMap = items.stream().collect(Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet())));
76 System.out.println(setMap);
77 //{19.99=[banana],
78 // 29.99=[orange, watermelon],
79 // 9.99=[papaya, apple]}