大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
展示如何使用Java 8 Stream Collectors
进行分组,计数,总和和排序List
分组,计数和排序
1.1分组List
并显示其总数。
package com.mkyong.java8; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Java8Example1 {
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); } }
Output
{ 番木瓜= 1,橙= 1,香蕉= 2,苹果= 3 }
1.2添加排序。
package com.mkyong.java8; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Java8Example2 {
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); } }
Output
{ 苹果= 3,香蕉= 2,木瓜= 1,橙= 1 }
2.List Objects
“分组”用户定义的对象列表的示例。
2.1 Pojo。
package com.mkyong.java8; import java.math.BigDecimal; public class Item {
private String name; private int qty; private BigDecimal price; //constructors, getter/setters }
2.2 按姓名+数字或数量组合。
package com.mkyong.java8; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Java8Examples3 {
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); } }
Output
// Group by + Count { 番木瓜= 1,香蕉= 2,苹果= 3,猩猩= 1,西瓜= 1 } // Group by + Sum qty { 番木瓜= 20,香蕉= 30,苹果= 40,orang = 10,西瓜= 10 }
2.2按价格分组 – Collectors.groupingBy
以Collectors.mapping
示例为例。
package com.mkyong.java8; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; public class Java8Examples4 {
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); } }
Output
{ 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 +映射到Set { 19.99 = [香蕉] 29.99 = [orang,西瓜], 9.99 = [番木瓜,苹果] }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/171267.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...