我想我曾经知道,但是我可能从此放弃,或者也许永远不知道。你知道为什么吗?
首先让我们在重写的方法上查看类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
。该库不仅可以处理MutablePair
,ImmutablePair
,还可以处理Map.Entry
的子类型。可能是您自定义的Map.Entry
实现。