Sure, here’s the translation: java negate boolean BiFunction

huangapple go评论72阅读模式
英文:

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 MathingTypes

public enum MatchingType {
    MATCH, MISMATCH
}

Map with matching type predicates

Map&lt;MatchingType, Function&lt;Set&lt;String&gt;, Predicate&lt;String&gt;&gt;&gt; predicateMap =
        Map.of(
                MATCH, set -&gt; set::contains,
                MISMATCH, set -&gt; not(set::contains)
        );

An example of usage

public boolean isKeyAvailable(String key, MatchingType matchingType, Set&lt;String&gt; 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&lt;MatchingType, BiFunction&lt;Set&lt;String&gt;, String, Boolean&gt;&gt; predicateMap =
        Map.of(
                MATCH, Set::contains,
                MISMATCH, Set::contains //how to negate?
        );

public boolean isKeyAvailable(String key, MatchingType matchingType, Set&lt;String&gt; 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&lt;Set&lt;String&gt;, String&gt; biPredicate = Set::contains;
    
Map&lt;MatchingType, BiPredicate&lt;Set&lt;String&gt;, String&gt;&gt; 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.

huangapple
  • 本文由 发表于 2020年9月18日 20:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63955767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定