栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA使用stream对数据分组后求每组的最大值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA使用stream对数据分组后求每组的最大值

JAVA stream对数据分组后求每组的最大值
具体需求

对industryCode分组,然后通过取出每组的num的最大值

// 实体类
@Data
@AllArgsConstructor
public class IndustryStockRelation {
    private Long id;
    private String industryCode;
    private Integer num;
}
// 具体实现
public static void main(String[] args) {
	List industryStockRelationList = new LinkedList<>();
	        IndustryStockRelation i1 = new IndustryStockRelation(1L,"100720401",3);
	        IndustryStockRelation i2 = new IndustryStockRelation(2L,"100720402",6);
	        IndustryStockRelation i3 = new IndustryStockRelation(2L,"100720401",9);
	        IndustryStockRelation i4 = new IndustryStockRelation(3L,"100720403",6);
	        industryStockRelationList.add(i1);
	        industryStockRelationList.add(i2);
	        industryStockRelationList.add(i3);
	        industryStockRelationList.add(i4);
	        Map map =
	                industryStockRelationList.parallelStream().collect(
	                        Collectors.groupingBy(IndustryStockRelation::getIndustryCode,
	                                Collectors.collectingAndThen(
	                                        Collectors.reducing((a,b)->a.getNum()> b.getNum()?a:b),Optional::get)
	
	                        )
	                );
}
// 结果
{
100720402=IndustryStockRelation(id=2, industryCode=100720402, num=6), 
100720403=IndustryStockRelation(id=3, industryCode=100720403, num=6), 
100720401=IndustryStockRelation(id=2, industryCode=100720401, num=9)
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号