简体   繁体   English

为什么Commons Lang Pair实现Map Entry?

[英]Why Commons Lang Pair implements Map Entry?

I think I once knew, but I might have ever since forgottoen, or maybe never knew.我想我曾经知道,但我可能从那以后就忘记了,或者永远不知道。 Do you know why?你知道为什么吗?

http://commons.apache.org/proper/commons-lang/javadocs/api-3.10/org/apache/commons/lang3/tuple/Pair.html http://commons.apache.org/proper/commons-lang/javadocs/api-3.10/org/apache/commons/lang3/tuple/Pair.ZFC35FDC70D5FC69D263988A3A

First let's have a look in to the class Pair at the overridden methods, it's nothing but delegations on the "native" getLeft() and getRight()首先让我们看看 class Pair在被覆盖的方法中,它只不过是“本机” getLeft()getRight()的委托

@Override
public final L getKey() {
    return getLeft();
}

@Override
public R getValue() {
    return getRight();
}

And now take a look at the static method of class ImmutablePair现在看看 class ImmutablePair的 static 方法

/**
 * <p>Creates an immutable pair from an existing pair.</p>
 *
 * <p>This factory allows the pair to be created using inference to
 * obtain the generic types.</p>
 *
 * @param <L> the left element type
 * @param <R> the right element type
 * @param pair the existing pair.
 * @return a pair formed from the two parameters, not null
 * @since 3.10
 */
public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
    final L left;
    final R right;
    if (pair != null) {
        left = pair.getKey();
        right = pair.getValue();
    } else {
        left = null;
        right = null;
    }
    return new ImmutablePair<>(left, right);
}

So no matter if you pass a type of the interface Map.Entry you will get a ImmutablePair back.所以无论你传递一个类型的接口Map.Entry你都会得到一个ImmutablePair回来。 The library can handle not only MutablePair , ImmutablePair but also what ever which is subtype of Map.Entry .该库不仅可以处理MutablePairImmutablePair ,还可以处理Map.Entry的子类型。 Maybe it could be your custom implementation of Map.Entry .也许它可能是您对Map.Entry的自定义实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM