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

时间:2020-06-07 09:14:06

标签: java spring-boot java-8

代码示例:-

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=demo@camunda.org) - userResponse object

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

接下来是else if条件,listGroup的响应为[UserResponse(status=null, id=demo, firstName=Demo, lastName=Demo, email=demo@camunda.org), UserResponse(status=null, id=mary, firstName=Mary, lastName=Anne, email=mary@camunda.org)]

所以现在您可以看到数据重复。我想要的输出是当userId不为空时,它应采用type并合并数组

否则,如果分组后不清空groupType所需的数据,并合并到数组中以删除重复数据并合并到同一对象中

输出:-

[
    {
        "userId": "demo",
        "name": "Demo Demo",
        "type": [
            "candidate",
            "assignee"
        ],
        "email": "demo@camunda.org",
        "groupId": [
            "accounting",
            "sales"
        ]
    },
    {
        "userId": "mary",
        "name": "Mary Anne",
        "type": [
            "candidate"
        ],
        "email": "mary@camunda.org",
        "groupId": [
            "accounting",
            "sales"
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

您需要对代码进行一些基本更改。

1-通过此更改,您可以不必使用ResponseEntity<String>依赖性,而不必使用ResponseEntity<UserCandidates[]> response来使用Gson()

2-您无需使用StringUtils来检查是否为空。 string list 对象都有相同的方法。

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());
}
相关问题