1. 分组, 计数和排序
1.1 分组, 计数
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></string>
输出 { papaya=1, orange=1, banana=2, apple=3 }
1.2 分组, 计数和排序
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); } </string></string></string></string>
输出: { apple=3, banana=2, papaya=1, orange=1 }
2.用户自定义对象集合分组, 计数、排序和求和
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); } </string></string></item>
输出 //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 } 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); } </string></string></item></item></item>
输出 { 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] }