我有一个String titleList<String> bannedSubstrings。现在,我想执行一行检查title是否没有这些bannedSubstrings

我的方法:

if(bannedSubstrings.stream().filter(bannedSubstring -> title.contains(bannedSubstring)).isEmpty()){
    ...
}


不幸的是,没有用于流的isEmpty()方法。那么您将如何解决该问题?有没有一线解决方案?

最佳答案

听起来您想在anyMatch上阅读:

if (bannedSubstrings.stream().anyMatch(title::contains)) {
    // bad words!
}


相反,还有noneMatch

if (bannedSubstrings.stream().noneMatch(title::contains)) {
    // no bad words :D
}


如果title是长字符串,这不是很有效(但是我想标题通常不应该长)。

09-10 22:31
查看更多