简体   繁体   English

基于java 8中的匹配属性比较两个不同对象类型的列表

[英]Compare two Lists of different object types based on matching property in java 8

I have two Lists of type:我有两个类型的列表:

class User {
    Long userId;
    String userName;
    Long workflowStatusId;
}

class WorkflowStatus {
    Long workflowStatusId;
    String workflowStatusName;
    Long userId;
}

Each user object have workflowStatusId.每个用户对象都有 workflowStatusId。 Similarly, each WorkflowStatus have userId associated with them.同样,每个 WorkflowStatus 都有与之关联的 userId。 I am saving User s first, which is giving me List<User> users where workflowStatusId is set as null for each object.我首先保存User ,这给了我List<User> users ,其中每个对象的workflowStatusId设置为 null 。 Now for each of these user in users, I am making new entries in WorkflowStatus which is giving me List<WorkflowStatus> workflowStatuses .现在,对于用户中的每个用户,我正在WorkflowStatus中创建新条目,这给了我List<WorkflowStatus> workflowStatuses Now I want to set these newly inserted workflowStatusId in my previously saved User object, so I am trying to match userId property in both and want to set this workflowStatusId once that match is found.现在我想在我之前保存的User对象中设置这些新插入的workflowStatusId ,所以我试图在两者中匹配userId属性,并希望在找到匹配项后设置这个workflowStatusId I am trying to do something like:我正在尝试做类似的事情:

List<User> filteredList = users
                                            .stream()
                                             .filter(user -> workflowStatuses
                                                                .stream()
                                                                .allMatch(user.getUserId().equals(workflowStatus.getUserId())))
                                                                .map(how to set workflowStatusId in user? can i do something like user->user.setWorkflowStatusId((workflowStatus.getWorkflowStatusId)))
                                            .collect(Collectors.toList()); 

Trying to find some solution, if it is possible to do using Java 8 streams .尝试找到一些解决方案,如果可以使用Java 8 streams What is the better way to Compare two Lists of different object types based on some common property in java 8?基于java 8中的一些共同属性比较不同对象类型的两个列表的更好方法是什么?

It is not a good idea to have side effects like setting of properties in the middle of a Stream.在 Stream 中间设置属性等副作用不是一个好主意。 A better solution is to just create a Map of your User instances:更好的解决方案是只创建用户实例的 Map:

Map<Long, User> usersById =
    users.stream().collect(Collectors.toMap(User::getUserId, u -> u));

Now you can simply loop through your WorkflowStatus objects:现在您可以简单地遍历您的 WorkflowStatus 对象:

for (WorkflowStatus status : workflowStatuses) {
    Long userId = status.getUserId();
    Long statusId = status.getWorkflowStatusId();
    usersById.get(userId).setWorkflowStatusId(statusId);
}
<User> filteredList = users.stream().filter(user -> workflowStatuses
                                                            .stream()
                                                            .allMatch(user.getUserId().equals(workflowStatus.getWorkflowStatusId)))
                                                            .map(how to set workflowStatusId in user? can i do something like user->user.setWorkflowStatusId((workflowStatus.getWorkflowStatusId)))
                                        .collect(Collectors.toList());

On this generate the lamba exprestion loop on java 8在此生成 java 8 上的 Lamba 表达式循环

workflowStatus.foreach( data ) -> { user.setworkflowStatusId(data.getWorkflowStatusId);user.setuserId(data.getuserId);}

Or simple replace below或在下面简单替换

<User> filteredList = users.stream().filter(user -> workflowStatuses
                                                        .stream()
                                                        .allMatch(user.getUserId().equals(workflowStatus.getWorkflowStatusId)))
                                                        .map(workflowStatus.foreach( data ) -> { user.setworkflowStatusId(data.getWorkflowStatusId);user.setuserId(data.getuserId)})
                                    .collect(Collectors.toList());

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

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