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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With