java如何将BiPredicate与anyMatch()一起使用
我有一组货币作为Set<String>
,需要的货币作为Set<String>
。我必须检查所需的货币是否存在于货币集合中。我已经为此写了BiPredicate
,如下所示,并试图在anyMatch()
中使用相同的内容。但这对我不起作用。我怎样才能做到这一点
Set<String> currencyValues = currencies.getCurrencies().values()
.stream()
.map(currencyEntity -> {
return currencyEntity.getNameOfSymbol();
}).collect(Collectors.toSet());
Set<String> requestCurrencyCodes = globalPricingRequests.stream().map(globalPricingRequest -> {
return globalPricingRequest.getCurrencyISOCode();
}).collect(Collectors.toSet());
BiPredicate<Set<String>, String> checkIfCurrencyPresent = Set::contains;
boolean isCurrencyCodeValid = requestCurrencyCodes.stream().anyMatch(checkIfCurrencyPresent.test(currencyValues));
我无法在^{
# 1 楼答案
你不需要一个
BiPredicate
。相反,一个简单的Predicate
就可以做到这一点这是一个更精简的版本
# 2 楼答案
再加上目前为止提供的好答案。您可以通过
Collections.disjoint
实现上述要求:如果
requestCurrencyCodes
中存在currencyValues
中的任何值,则isCurrencyCodeValid
将为真,否则为假完整代码:
# 3 楼答案
^{} 方法采用的是
Predicate
,而不是BiPredicate
。因此,不能将BiPredicate
直接与anyMatch
一起使用。从您展示的代码来看,您无论如何都不需要BiPredicate
。只要做:如果出于某种原因确实想使用
BiPredicate
,可以执行以下操作:然而,我不知道你为什么要这么做。它所做的只是将一个
BiPredicate
包装在一个Predicate
中# 4 楼答案
虽然理想情况下,我最好在这里使用preferred using ^{} ,但是如果您要创建一个可在多种情况下使用的通用方法,您可以使用以下实用方法避免在
Predicate
中使用BiPredicate
的包装:然后使用lambda将其消费为:
这样,它就不依赖于针对特定的
Set
测试字符串,并且您可以将其广泛地用作:旁白:代码的前两行可能会变得更可读,例如(假设模型名):