ImmutablePair 哈希码
ImmutablePair Hashcode
我需要使用 ImmutablePair. But it seems that its hashcode is defined as this: https://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/tuple/Pair.html#line.208。这意味着 ImmutablePair.of("a", "a")
和 ImmutablePair.of("b", "b")
将具有相同的 hashCode 0
:
ImmutablePair<String, String> p1 = ImmutablePair.of("a", "a");
System.out.println("Pair 1 hashcode: " + p1.hashCode());
ImmutablePair<String, String> p2 = ImmutablePair.of("b", "b");
System.out.println("Pair 2 hashcode: " + p2.hashCode());
输出:
Pair 1 hashcode: 0
Pair 2 hashcode: 0
这对我来说似乎很奇怪。有人可以解释一下这样做的合理性吗?
如方法定义的注释中所述,Map.Entry
的合同要求此实现,ImmutablePair
实现。
我需要使用 ImmutablePair. But it seems that its hashcode is defined as this: https://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/tuple/Pair.html#line.208。这意味着 ImmutablePair.of("a", "a")
和 ImmutablePair.of("b", "b")
将具有相同的 hashCode 0
:
ImmutablePair<String, String> p1 = ImmutablePair.of("a", "a");
System.out.println("Pair 1 hashcode: " + p1.hashCode());
ImmutablePair<String, String> p2 = ImmutablePair.of("b", "b");
System.out.println("Pair 2 hashcode: " + p2.hashCode());
输出:
Pair 1 hashcode: 0
Pair 2 hashcode: 0
这对我来说似乎很奇怪。有人可以解释一下这样做的合理性吗?
如方法定义的注释中所述,Map.Entry
的合同要求此实现,ImmutablePair
实现。