-1

我想我曾经知道,但我可能从那以后就忘记了,或者永远不知道。你知道为什么吗?

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

4

1 回答 1

-3

首先让我们看一下Pair覆盖方法的类,它只不过是“本机”getLeft()getRight()

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

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

现在看看类的静态方法ImmutablePair

/**
 * <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);
}

所以无论你传递一个类型的接口Map.Entry你都会得到ImmutablePair回报。该库不仅可以处理MutablePairImmutablePair还可以处理任何子类型Map.Entry。也许它可能是您的自定义实现Map.Entry

于 2020-06-01T21:38:35.877 回答