繁体   English   中英

根据匹配属性将对象分组到 java 8 或 java 11 中的列表中

[英]group objects based on matching properties into list in java 8 or java 11

我的课程类似于:

class Response {
    Source source;
    Target target;
}

class Source {
    Long sourceId;
    String sourceName;
}

class Target {
    Long regionId;
    String regionName;
    Long countryId;
    String countryName;
}

在响应中,对于不同的目标 object,source(sourceId,sourceName) 可能相同。 同样,我也想根据regionIdregionNameTarget Object 内进行分组。 对于regionIdregionName的组合,我们可以在目标 Object 中获得countries List

我在数据库中有这 6 个属性sourceId, sourceName, targetId, targetName,countryId,countryName 我可以在多行上拥有相同的sourceId, sourceNametarget总是不同的。

我想将所有目标对象分组到源相同的列表中。

我有响应对象列表,我正在尝试对其执行 stream() 操作,例如:

List<Response> responses; // set up the input list

List<FinalResponse> finalResponseLst = responses.stream()
    .collect(Collectors.groupingBy(
        Response::getSource,
        Collectors.mapping(Response::getTarget, Collectors.toList())
    )) 
    .entrySet()
    .stream() 
    .map(e -> new FinalResponse(e.getKey(), e.getValue()))
    .collect(Collectors.toList());

这给了我Source和它们各自的Target对象。 但是如何根据地区对目标对象内的国家进行分组呢? 如何为单个Target Object 创建具有相同地区的国家/地区列表。 所以我的最终 output JSON 看起来像:

Response": { 
    "Source":       {  
        "sourceId":       "100",   
        "sourceName":      "source1",    
    },
    "Targets": [
    {
    "TargetRegion":{//grouping target objects with same regions
        "regionId":       "100",   
        "regionName":      "APAC",          
    }, 
    "TargetCountries":[{
        "countryId":       "1",   
        "countryName":      "USA",   
    },
    {
        "targetId":       "2",   
        "targetName":      "China",   
    },
    {
        "targetId":       "3",   
        "targetName":     "Brazil"   
    }
    ]
    },
    {
    "TargetRegion":{//grouping target objects with same regions
       
        "regionId":       "200",   
        "regionName":      "ASPAC",         
    }, 
    "TargetCountries":[{
        "countryId":       "11",   
        "countryName":      "Japan",   
    },
    {
        "targetId":       "22",   
        "targetName":      "Combodia",   
    },
    {
        "targetId":       "33",   
        "targetName":     "Thailand"   
    }
    ]
    }

    ]
}

您应该只添加额外的分组并确保存在适当的 POJO 类:

  • POJO类:
@Data
@AllArgsConstructor
class Region {
    Long regionId;
    String regionName;
}

@Data
@AllArgsConstructor
class Country {
    Long countryId;
    String countryName;
}

@Data
@AllArgsConstructor
class RegionCountries {
    private Region targetRegion;
    private List<Country> targetCountries;
}

@Data
@AllArgsConstructor
class FinalResponse {
    Source source;
    List<RegionCountries> targets;
}

那么collection可以实现如下:

List<FinalResponse> f = responses.stream()
        .collect(Collectors.groupingBy(
                Response::getSource,
                Collectors.groupingBy(
                        r -> new Region(
                            r.getTarget().getRegionId(),
                            r.getTarget().getRegionName()
                        ),
                        Collectors.mapping(r -> new Country(
                                r.getTarget().getCountryId(), 
                                r.getTarget().getCountryName()
                            ),
                            Collectors.toList()
                        )
                )
        )) // Map<Source, Map<Region, List<Country>>>
        .entrySet()
        .stream()
        .map(e -> new FinalResponse(
                e.getKey(),
                e.getValue().entrySet()
                 .stream()
                 .map(re -> new RegionCountries(re.getKey(), re.getValue()))
                 .collect(Collectors.toList()) // List<RegionCountries>
        ))
        .collect(Collectors.toList());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM