Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

java - If any match found in the stream set data to innerobject of the first object in a list

GroupNode Class:

@Data
public class GroupNode {
    private String groupName;
    private List<PolicyNode> policies;
    private String eventId;
}

PolicyNode Class:

@Data
public class PolicyNode {
    private String policyName;
    private String eventTime;
    private String eventId;
}

Here i want is if eventId of groupNode matches with the eventId of policyNode then i want to set the policyNode details in groupNode.

the code i've tried.

List<GroupNode> groupWithPolicy = groupWithoutPolicy.stream()
    .filter(gwp->groupPolicyNodeList.stream().anyMatch(gwop->gwop.getEventId().equals(gwp.getEventId())))
                .collect(Collectors.toList());

I know that i'm not setting any property here but I'm not getting any idea how to set those properties to a group node:/

this is the sample response i want:

"groups": [
            {
                "groupName": "My group",
                "policies": [
                    {
                        "policyName": "Inno-Test-Cust-Man-Policy",
                        "eventId": "67e3426e-fd42-483f-9d90-8f7d15646f6a",
                    },
                    {
                        "policyName": "Inno-Test-Cust-Man-Policy",
                        "eventId": "67e3426e-fd42-483f-9d90-8f7d15646f6a",
                    }
                ],
                "eventId": "2d72752d-8f28-4549-9c0c-986ac98873cf"
            }
        ]
    }

I removed lot of data from above output here the eventId's are not matching. so i've all groups list without policies attached to it. and all policies list. now based on eventId i want to add policies to the group.

question from:https://stackoverflow.com/questions/65868432/if-any-match-found-in-the-stream-set-data-to-innerobject-of-the-first-object-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it for example so:

public static void main(String[] args) {
    List<GroupNode> groupWithPolicy = groupWithoutPolicy.stream()
            .map(s -> setPolicyNodeList(s, externalPolicyList))
            .collect(Collectors.toList());

}

private static GroupNode setPolicyNodeList(GroupNode groupNode, List<PolicyNode> policyNodeList) {
    groupNode.setPolicyNode (
            policyNodeList.stream()
            .filter(s-> s.getEventId.equals(groupNode.getEventId))
            .collect(Collectors.toList())
    );

    return groupNode;
}

it's easy to read and understanding.

Also, you can do grouping and escape from O(n*n):

    Map<String, List<PolicyNode>> collect = policyNodeList.stream()
            .collect(groupingBy(s -> s.getEventId));
    List<GroupNode> groupWithPolicy = groupWithoutPolicy.stream()
            .peek(s -> s.setPolicies(collect.get(s.getEventId())))
            .collect(Collectors.toList());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...