英文:
java negate boolean BiFunction
问题
我有一个类,它将检查一个键是否包含/不包含在一组键中。这些条件在Map
中用MatchingType
进行描述。
public enum MatchingType {
MATCH, MISMATCH
}
具有匹配类型断言的Map
Map<MatchingType, Function<Set<String>, Predicate<String>>> predicateMap =
Map.of(
MATCH, set -> set::contains,
MISMATCH, set -> not(set::contains)
);
用法示例
public boolean isKeyAvailable(String key, MatchingType matchingType, Set<String> keys) {
return predicateMap.get(matchingType)
.apply(keys)
.test(key);
}
现在我发现可以使用BiFunction
重构上面的代码。
Map<MatchingType, BiFunction<Set<String>, String, Boolean>> predicateMap =
Map.of(
MATCH, Set::contains,
MISMATCH, (set, str) -> !set.contains(str)
);
public boolean isKeyAvailable(String key, MatchingType matchingType, Set<String> keys) {
return predicateMap.get(matchingType)
.apply(keys, key);
}
但是如何否定Set::contains
呢?
英文:
I have a class which will check if a key is/isn't included into a set of keys. This conditions are described in Map
with use of MathingType
s
public enum MatchingType {
MATCH, MISMATCH
}
Map with matching type predicates
Map<MatchingType, Function<Set<String>, Predicate<String>>> predicateMap =
Map.of(
MATCH, set -> set::contains,
MISMATCH, set -> not(set::contains)
);
An example of usage
public boolean isKeyAvailable(String key, MatchingType matchingType, Set<String> keys) {
return predicateMap.get(matchingType)
.apply(keys)
.test(key);
}
Now I see that code from above is possible to refactor with the use of BiFunction
.
Map<MatchingType, BiFunction<Set<String>, String, Boolean>> predicateMap =
Map.of(
MATCH, Set::contains,
MISMATCH, Set::contains //how to negate?
);
public boolean isKeyAvailable(String key, MatchingType matchingType, Set<String> keys) {
return predicateMap.get(matchingType)
.apply(keys, key);
}
But how does it possible to negate Set::contains
?
答案1
得分: 1
截至[tag:java-11],存在一个静态方法Predicate.not(Predicate)
,然而,在BiPredicate
中却没有这样的方法。
您可能希望直接使用BiPredicate
,并使用其实例方法negate
,该方法从[tag:java-8]开始发布:
BiPredicate<Set<String>, String> biPredicate = Set::contains;
Map<MatchingType, BiPredicate<Set<String>, String>> biPredicateMap =
Map.of(
MatchingType.MATCH, biPredicate,
MatchingType.MISMATCH, biPredicate.negate()
);
boolean result = biPredicateMap.get(matchingType)
.test(keys, key);
基于Function
的函数接口没有否定操作,因为它们的lambda表达式不能保证返回Boolean
。
英文:
As of [tag:java-11] there is a static method Predicate.not(Predicate)
however, there is not such method in BiPredicate
.
You might want to use BiPredicate
as is with its instance method negate
, which is available since its release as of [tag:java-8]:
BiPredicate<Set<String>, String> biPredicate = Set::contains;
Map<MatchingType, BiPredicate<Set<String>, String>> biPredicateMap =
Map.of(
MatchingType.MATCH, biPredicate,
MatchingType.MISMATCH, biPredicate.negate()
);
boolean result = biPredicateMap.get(matchingType)
.test(keys, key);
The Function
based functional interface don't have negation since their lambda expressions are not guaranteed to return Boolean
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论