简体   繁体   中英

Java build a list from two other lists with different objects and common property

I am trying to build a list with a common property from two other lists of different objects. I am using the Stream library but I can not make the code work. Here is what I have tried:

Attempt 1 (this returns only the first element of the ObjectAList):

private List<ObjectA> getFilteredObjectAList() {

    return ObjectAList.stream()
            .filter(a -> a.getProperty().getPropertyId().equals(
                    (ObjectBList.stream().map(b -> b.getPropertyId())).findAny().orElse(null)))
            .collect(Collectors.toList());
}

Attempt 2 (this raises error "The target type of this expression must be a functional interface"):

private List<ObjectA> getFilteredObjectAList() {
     List<ObjectA> list;
     ObjectAList.stream()
     .forEach(a -> a.getProperty().getPropertyId()
     .filter((ObjectBList.stream()
     .map((b -> b.getPropertyId())::contains ? list.add(t) : "".isEmpty())))
     .collect(Collectors.toList()));
     return list;
     }

Your help is so much appreciated.

I think you want to return ObjectA instances where the value getProperty().getPropertyId() exists in some list of ObjectB values.

Your first attempt doesn't work because it compares getPropertyId of each ObjectA with getPropertyId of a random ObjectB (because you're calling findAny ).

You want something like this:

private List<ObjectA> getFilteredObjectAList() {
    return ObjectAList.stream()
            .filter(a -> ObjectBList.stream().matchAny(b -> b.getPropertyId() ==
                a.getProperty().getPropertyId())
            .collect(Collectors.toList());
}

However, this approach could be slow if the lists are large, because you're scanning the ObjectB list once for very member of the ObjectA list. You're better off constructing a HashSet of the IDs that exist in the ObjectB list, then looking up the values from the ObjectA list in that set.

private List<ObjectA> getFilteredObjectAList() {
    Set<String> ids = ObjectBList.stream().map(ObjectB::getPropertyId)
        .collect(Collectors.toCollection(HashSet::new));
    return ObjectAList.stream()
            .filter(a-> ids.contains(a.getProperty().getPropertyId())
            .collect(Collectors.toList());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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