jdk8 stream项目使用

分组

原始数据

ItemType{itemType='9折劵-没有享受优惠',groupName='掌厅'}, ItemType{itemType='9折劵-用户未成功抢购'groupName='掌厅'}, 
ItemType{ itemType='充值-充值未到账',groupName='套餐流量阿米巴'}, ItemType{itemType='充值-融合资费缴费'groupName='套餐流量阿米巴'}

需要数据

{name: '销售员', children: [
			{name: '张三1', value: 1, selected: true},
			{name: '李四1', value: 2, selected: true},
			{name: '王五1', value: 3, disabled: true},
		]},

处理过程

List<Map<String, Object>> list =
					itemTypes.stream().collect(Collectors.groupingBy(ItemType::getGroupName)).entrySet().stream().map(stringListEntry -> {
						Map<String, Object> hashMap = new HashMap<>();
						hashMap.put("name", stringListEntry.getKey());
						List<Map<String, String>> childrens = new ArrayList<>();
						stringListEntry.getValue().stream().forEach(itemType -> {
							Map<String, String> map = new HashMap<>();
							map.put("name", itemType.getItemType());
							map.put("value", itemType.getItemType());
							childrens.add(map);
						});
						hashMap.put("children", childrens);
						return hashMap;
					}).collect(Collectors.toList());

过滤

list.stream().filter(complain -> complain.getProduct() == null).forEach(complain -> {})
作者: JaminYe
版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文地址:https://www.cnblogs.com/JaminYe/p/13940700.html