I think I once knew, but I might have ever since forgottoen, or maybe never knew. Do you know why?
Asked
Active
Viewed 452 times
-1
-
Maybe check out the repo https://github.com/apache/commons-lang and dig into it. – Quang Nguyen Jun 01 '20 at 21:15
-
The only thing that comes to my mind is this `map.entrySet().stream().anyMatch(pair::equals)` – Kamil Bęben Jun 01 '20 at 21:25
-
and it seems like it is the only thing that they are testing, so maybe that was actually their intention ([test](https://github.com/apache/commons-lang/blob/master/src/test/java/org/apache/commons/lang3/tuple/PairTest.java)) – Kamil Bęben Jun 01 '20 at 21:32
1 Answers
-3
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()
@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
/**
* <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. The library can handle not only MutablePair
, ImmutablePair
but also what ever which is subtype of Map.Entry
. Maybe it could be your custom implementation of Map.Entry
.

Quang Nguyen
- 354
- 1
- 5
- 20
-
-
Yeah, I was about to ask "so what" or something. Interesting trivia anyway, specially (for me) method `of(1)`. – 1737973 Jun 03 '20 at 15:25