我有一个String title
和List<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
是长字符串,这不是很有效(但是我想标题通常不应该长)。