Is there a way to merge two for loops using Streams in java 8?

Jeeva D :

I have two lists in which one is of type String and the other is of some entity object. How to iterate through those two lists or compare it by using java 8

List<Admin> admin= new ArrayList<>();

for (Admin ah : subProducers) {
    for (String value : values) {
        if (ah.getFirstName().contains(value) || ah.getLastName().contains(value)) {
            admin.add(ah);
        }
    }
}

I am currently using the for loop to verify that condition, I don't find any better way to combine it using java 8 streams.

Naman :

Something like an anyMatch with nested streams :

subProducers.stream()
            .filter(a -> values.stream()
                               .anyMatch(b -> a.getFirstName().contains(b)
                                           || a.getLastName().contains(b)))
            .collect(Collectors.toList())

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=75007&siteId=1