集合(Map,List)分组:多属性进⾏分组
集合(Map,List)分组:多属性进⾏分组
⼀、List 实体字段分组
1. 根据单⼀字段进⾏分组:
Map<Integer, List<SomeEntity>> detailTypeMap = SomeEntityS.stream().upingBy(SomeEntity::getSomeProperty));
2. 根据⽇期字段的 yyyy-MM 进⾏分组:
Map<String, List<SomeEntity>> monthMap = someEntityList.stream().upingBy(p -> date.DateUtil.OrderTime(), "yyyy-MM")));
3.1 根据 Entigy多个字段,拼成⼀个进⾏分组:
Map<String, Map<Integer, List<SomeEntity>>> someEntityGroup = someEntityList.stream().upingBy(SomeEntity::getSomePropertyOne, upingBy(SomeEntity::getSomePropertyTwo)));
3.2 根据 Entigy多个字段,分开进⾏分组:groupby分组
Map<Integer, Map<Integer, Map<Integer, List<SomeEntity>>>> someEntityGroup = someEntityList.stream().collect(
3.3 根据多字段分组,引⼊⼀个包括分组字段的对象,进⾏分组:
1    @Data
2    @Builder
3    @EqualsAndHashCode
4    @AllArgsConstructor
5    @NoArgsConstructor
6public class UseGroupEntity {
7
8/** 某属性 */
9private Integer somePropertyOne;
10
11/** 某属性 */
12private Integer somePropertyTwo;
13
14/** 某属性 */
15private String somePropertyThree;
16
17    }
18
19if(CollectionUtils.isNotEmpty(someEntityList)) {
20        someEntityGroup = someEntityList.stream().upingBy(entity -> {
21return UseGroupEntity.builder().SomePropertyOne()).SomePropertyTwo()).SomePropertyThree()).build();
22        }));
23    }
⼆、Map 多个key进⾏分组
4. 根据 Map中的多个字段组合进⾏分组:
【写法1】:
1        Map<String, List<Map<String, String>>> mapListGroup = new HashMap();
2        mapListGroup = someMapList.stream().upingBy(dataMap -> {
3            String groupByField = "";
4            StringBuilder groupKey = new StringBuilder();
ains(",")){        // groupByBussTypeField 以逗号分隔的多分组字段的配置值例 t1.name,t1.age,t2.score
6                String[] fields = groupByBussTypeField.split(",");
7for(String field : fields){
8                    String groupField = field.substring(field.indexOf(".") + 1);    // 将 t1.name,t1.age,t2.score 的点号前的前缀切去,获得单独的属性 name 和 age 和 score
9                    groupKey.append("\"").append(groupField).append("\"").append(":").append("\"").append(String.(groupField))).append("\"");
10                }
11                groupByField = String();
12            }else{
13                groupByField = String.(groupByBussTypeField.substring(groupByBussTypeField.indexOf(".") + 1)));
14            }
15return groupByField;
16        }));
【写法2】:
1        Map<String, List<Map<String, String>>> voucherGroup = someMapList.stream().upingBy((e -> fetchGroupKey(someEntityProperties, e))));
2
3private String fetchGroupKey(List<SomeEntityProperty> someEntityProperties, Map<String, String> dataMap){
4// log.debug("fetchGroupKey ⼊参:someEntityProperties:{},dataMap:{}", JSONString(someEntityProperties), JSONString(dataMap));
5if(CollectionUtils.isEmpty(collection)){
6                String errorMsg = "数据为为空,请核查!";
7                (errorMsg);
8throw new RuntimeException(errorMsg);
9            }
10            StringBuilder groupKey = new StringBuilder();
11            someEntityProperties.forEach(someEntityProperty -> {
12                String fieldNameByField = Name();
13                groupKey.append("\"").append(fieldNameByField).append("\"").append(":").append("\"").append(String.(fieldNameByField))).append("\"");
14            });
15// log.debug("依多属性字段分组,分组依据为:{}", String());
String();
17        }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。