简体   繁体   English

有没有办法在java 8中使用Streams合并两个for循环?

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

I have two lists in which one is of type String and the other is of some entity object. 我有两个列表,其中一个是String类型,另一个是一些实体对象。 How to iterate through those two lists or compare it by using java 8 如何迭代这两个列表或使用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. 我目前正在使用for循环来验证这个条件,我找不到任何更好的方法来使用java 8流来组合它。

Something like an anyMatch with nested streams : 类似于具有嵌套流的anyMatch

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

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

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