吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1355|回复: 0
收起左侧

[Java 转载] 【笔记】JAVA 1.8 Collectors.groupingBy

[复制链接]
rwkk12138 发表于 2021-9-18 10:28
[Java] 纯文本查看 复制代码
Map<String, List<Product>> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory));
            Map<String, List<Product>> prodMap = prodList.stream().collect(Collectors.groupingBy(item -> item.getCategory() + "_" + item.getName()));
Map<String, List<DdDistrict>> dMap = find(ddDistrict).stream().collect(
				Collectors.groupingBy(DdDistrict::getParentId, LinkedHashMap::new, Collectors.toList()));
// 按照条件分组
[Java] 纯文本查看 复制代码
Map<String, Map<String, List<Product>>> prodMap= prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.groupingBy(item -> {
    if(item.getNum() < 3) {
        return "3";
    }else {
        return "other";
    }
})));
//{"啤酒":{"other":[{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}]},"零食":{"other":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30}],"3":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}}

// 要实现多级分组,我们可以使用一个由双参数版本的Collectors.groupingBy工厂方法创 建的收集器,它除了普通的分类函数之外,还可以接受collector类型的第二个参数。那么要进 行二级分组的话,我们可以把一个内层groupingBy传递给外层groupingBy,并定义一个为流 中项目分类的二级标准。
[Java] 纯文本查看 复制代码
Map<String, List<Product>> prodMap= prodList.stream().collect(Collectors.groupingBy(item -> {
    if(item.getNum() < 3) {
        return "3";
    }else {
        return "other";
    }
}));

//{"other":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30},{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"3":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}

[Java] 纯文本查看 复制代码
// 按子组收集数据
// 求总数
Map<String, Long> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.counting()));
[mw_shl_code=java,true]// 把收集器的结果转换为另一种类型
Map<String, Product> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Product::getNum)), Optional::get)));

//{"啤酒":{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15},"零食":{"category":"零食","id":3,"name":"月饼","num":3,"price":30}}

// 联合其他收集器
Map<String, Set<String>> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.mapping(Product::getName, Collectors.toSet())));

//{"啤酒":["青岛啤酒","百威啤酒"],"零食":["面包","饼干","月饼"]}

// 多层分组
/* Map>>  按用户分组,按类型分组,组装:每个用户、每个类型下的  属性值列表*/
Map>> userAttrMap = userAttrList.stream().collect(
                Collectors.groupingBy(IapUserIndustryAttrRel :: getUserId,
                        Collectors.groupingBy(IapUserIndustryAttrRel :: getIndustryTypeId,
                                Collectors.mapping( IapUserIndustryAttrRel :: getIndustryAttributeId, Collectors.toList() )
                                )
                        )
                );
// 多层分组示例
    public static void main(String[] args) {
		List<IapUserIndustryAttrRel> userAttrList = new ArrayList<>();
		
		IapUserIndustryAttrRel userAttr1 = new IapUserIndustryAttrRel();
		userAttr1.setUserId("100001");
		userAttr1.setIndustryTypeId("1");
		userAttr1.setIndustryAttributeId("1");
		userAttrList.add(userAttr1);
		
		IapUserIndustryAttrRel userAttr2 = new IapUserIndustryAttrRel();
		userAttr2.setUserId("100001");
		userAttr2.setIndustryTypeId("1");
		userAttr2.setIndustryAttributeId("2");
		userAttrList.add(userAttr2);
		
		IapUserIndustryAttrRel userAttr3 = new IapUserIndustryAttrRel();
		userAttr3.setUserId("100001");
		userAttr3.setIndustryTypeId("2");
		userAttr3.setIndustryAttributeId("3");
		userAttrList.add(userAttr3);
		
		Map<String, Map<String, List<String>>> userAttrMap = userAttrList.stream().collect(
				Collectors.groupingBy(IapUserIndustryAttrRel :: getUserId,
						Collectors.groupingBy(IapUserIndustryAttrRel :: getIndustryTypeId,
								Collectors.mapping(IapUserIndustryAttrRel :: getIndustryAttributeId, Collectors.toList())
								)
						)
				);
		
		System.out.println(userAttrMap);
	
	}
// 输出结果:{100001={1=[1, 2], 2=[3]}}

// Map>> 按机构号分组,按渠道号分组,组装:每个机构、每个渠道下的  产品信息(map)
        Test t1= new Test("001","1","Y1","1");
        Test t2= new Test("001","2","Y1","2");
        Test t3= new Test("002","1","Y1","3");
        Test t4= new Test("002","2","Y1","4");
        Test t5= new Test("001","1","Y2","5");
        Test t6= new Test("002","1","Y2","6");
	    List<Test> list = new ArrayList<>();
	    list.add(t1);
        list.add(t2);
        list.add(t3);
        list.add(t4);
        list.add(t5);
        list.add(t6);
        Map<String, Map<String, Map<String, String>>> collect = list.stream().collect(Collectors.groupingBy(Test::getOrgCode, Collectors.groupingBy(Test::getChannelId, Collectors.toMap(Test::getProductCode, Test::getD))));
        System.out.println(JSON.toJSON(collect));
// 输出结果:{"001":{"1":{"Y1":"1","Y2":"5"},"2":{"Y1":"2"}},"002":{"1":{"Y1":"3","Y2":"6"},"2":{"Y1":"4"}}}

//{"啤酒":2,"零食":3}[/mw_shl_code]
[Java] 纯文本查看 复制代码
// 求和
Map<String, Integer> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.summingInt(Product::getNum)));

//{"啤酒":13,"零食":6}

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-7 12:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表