列表包含来自另一个列表的至少一个值(Java 8) [英] A list contains at least one value from another list (Java 8)

查看:177
本文介绍了列表包含来自另一个列表的至少一个值(Java 8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数查找是否从列表中的给定单词列表中,一个单词在页面中有效。
因此,当页面包含单词时,我将其编写如下:
(简化版)

My function finds if from a list of given words at list one word is valid in the page. So when page contains words I have written it as follows: (Simplified version)

private boolean atLeastOneWordIsValidInThePage(Page page, Set<Long>  wordIdsToCheck) 
    Set<Long> wordIds = page.getWords().stream()
                .filter(word -> word.isValid())
                .map(word->getWordId())
                .collect(Collectors.toSet());
return words.stream().anyMatch((o) -> wordIds.contains(o));

编写它是最好的java 8练习吗?

我想要找到第一个匹配时停止搜索。

Is it the best java 8 practice to write it?
I want to stop searching when the first match is found.

推荐答案

无需打开两个单独的流。您应该能够将 anyMatch 直接链接到 map 函数,如下所示:

There is no need to open two separate streams. You should be able to chain anyMatch directly to the map function as follows :

return page.getWords().stream()
            .filter(word -> word.isValid())
            .map(word->getWordId())
            .anyMatch((o) -> words.contains(o));

这篇关于列表包含来自另一个列表的至少一个值(Java 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆