从对象中删除重复项并合并到数组中

塔尼克·奈克(Tanik Naik):

代码示例:-

public List<UserDto> getUserCandidates(String taskId) {
        List<UserCandidates> listResponse;
        ResponseEntity<String> response=restTemplate.getForEntity(configProperties.getUrl()+"/task/"+taskId+"/identity-links",
                String.class);      
        listResponse =new Gson().fromJson(response.getBody(), new TypeToken<ArrayList<UserCandidates>>(){}.getType());
        listResponse.forEach(result->{
            if(!StringUtils.isEmpty(result.getUserId())){
                ResponseEntity<UserRefer> userResponse=restTemplate.getForEntity(configProperties.getUrl()+"/user/"+result.getUserId()+"/profile", UserRefer.class);
                userDtoList.add(new UserDto(result.getUserId(), Arrays.asList(result.getGroupId()), Arrays.asList(result.getType()), userResponse.getBody().getFirstName(), 
                        userResponse.getBody().getLastName(), userResponse.getBody().getEmail()));
            }
            else if(!StringUtils.isEmpty(result.getGroupId())) {
                ResponseEntity<String> responseGroup=restTemplate.getForEntity(configProperties.getUrl()+"/user"+"?memberOfGroup="+result.getGroupId(), String.class);
                List<UserResponse> listGroup=new Gson().fromJson(responseGroup.getBody(), new TypeToken<ArrayList<UserResponse>>(){}.getType());
                listGroup.forEach(resultGroup->{
                    userDtoList.add(new UserDto(resultGroup.getId(),Arrays.asList(result.getGroupId()),
                            Arrays.asList(result.getType()),resultGroup.getFirstName(),resultGroup.getLastName(),resultGroup.getEmail()));
                });
            }    

        });
        return userDtoList;
    }

因此,如果条件满足,我从API得到的响应是

UserRefer(id=demo, firstName=Demo, lastName=Demo, [email protected]) - userResponse object

从listResponse对象数据是 [UserCandidates(userId=null, groupId=accounting, type=candidate), UserCandidates(userId=null, groupId=sales, type=candidate), UserCandidates(userId=demo, groupId=null, type=assignee)]

否则,如果条件为listGroup的响应为 [UserResponse(status=null, id=demo, firstName=Demo, lastName=Demo, [email protected]), UserResponse(status=null, id=mary, firstName=Mary, lastName=Anne, [email protected])]

因此,现在您可以看到数据是重复的。我想要的输出是针对当userId不应该为空的数据,并且应该type合并数组

否则,如果分组后不清空数据,则应将其获取groupType并合并到数组中,以删除重复数据并合并到同一对象中

输出:-

[
    {
        "userId": "demo",
        "name": "Demo Demo",
        "type": [
            "candidate",
            "assignee"
        ],
        "email": "[email protected]",
        "groupId": [
            "accounting",
            "sales"
        ]
    },
    {
        "userId": "mary",
        "name": "Mary Anne",
        "type": [
            "candidate"
        ],
        "email": "[email protected]",
        "groupId": [
            "accounting",
            "sales"
        ]
    }
]
哈迪J:

您需要对代码进行一些根本性的更改。

1- 不需要通过ResponseEntity<String>使用ResponseEntity<UserCandidates[]> response此更改来使用use ,您不需要使用Gson()依赖项。

2-您不需要使用StringUtils来检查是否为空。字符串列表对象都有相同的方法

3-对于重复的日期,我定义了一个Map<String,UserDto>ID为键的键,userDto对象为一个值。以及userDto创建数据的位置,我将其存储在具有ID的地图中。如您userDto在地图中存储对象所见,我使用的merge方法对于重复的key(id)具有合并功能。

提示:出于可读性考虑,最好将restTemplate调用分隔在其他类中,也可以重用它。

mergeFunction是这样的:

private UserDto mergeFunction(UserDto u1,UserDto u2){
    u1.getType().addAll(u2.getType());
    u1.getGroupId().addAll(u2.getGroupId());
    return u1;
 }  

完整的代码是:

public List<UserDto> getUserCandidates(String taskId) {

    Map<String, UserDto> userDtoMap = new HashMap<>();
    Map<String, String> params = new HashMap<>();

    ResponseEntity<UserCandidates[]> response = restTemplate
         .getForEntity(configProperties.getUrl() + "/task/" + taskId + "/identity-links",
                    UserCandidates[].class, params);

    Arrays.asList(response.getBody()).forEach(result -> {
        if (!result.getUserId().isEmpty()) {
            ResponseEntity<UserRefer> userResponse = restTemplate
                  .getForEntity(configProperties.getUrl() + "/**", UserRefer.class);

            userDtoMap.merge(result.getUserId(), new UserDto(result.getUserId(),
                    new ArrayList<>(Arrays.asList(result.getGroupId())), Arrays.asList(result.getType()),
                    userResponse.getBody().getFirstName(),
                    userResponse.getBody().getLastName(),
                    userResponse.getBody().getEmail()), (u1, u2) -> mergeFunction(u1,u2));
        } else if (!result.getGroupId().isEmpty()) {

            String requestUri = configProperties.getUrl() + "/user" +
                                   "?memberOfGroup={memberOfGroup}";
            Map<String, String> userResParam = new HashMap<>();
            userResParam.put("memberOfGroup", result.getGroupId());
            ResponseEntity<UserResponse[]> responseGroup = restTemplate
                    .getForEntity(requestUri, UserResponse[].class, userResParam);

            Arrays.asList(responseGroup.getBody()).forEach(resultGroup -> {
                userDtoMap.merge(resultGroup.getId(), new UserDto(resultGroup.getId(),
                        Arrays.asList(result.getGroupId()),
                        Arrays.asList(result.getType()), resultGroup.getFirstName(),
                        resultGroup.getLastName(),
                        resultGroup.getEmail()), (u1, u2) -> mergeFunction(u1,u2));
            });
        }

    });
    return new ArrayList<>(userDtoMap.values());
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章