根据 UUID 标准RFC 4122,UUID 应被视为无符号整数 128,并且应按以下方式进行比较:
Rules for Lexical Equivalence:
Consider each field of the UUID to be an unsigned integer as shown
in the table in section Section 4.1.2. Then, to compare a pair of
UUIDs, arithmetically compare the corresponding fields from each
UUID in order of significance and according to their data type.
Two UUIDs are equal if and only if all the corresponding fields
are equal.
UUIDs, as defined in this document, can also be ordered
lexicographically. For a pair of UUIDs, the first one follows the
second if the most significant field in which the UUIDs differ is
greater for the first UUID. The second precedes the first if the
most significant field in which the UUIDs differ is greater for
the second UUID.
意思是 println(UUID.fromString("b533260f-6479-4014-a007-818481bd98c6") < UUID.fromString("131f0ada-6b6a-4e75-a6a0-4149958664e3"))
应该打印 false。
然而,它打印的是真的!!
查看 compareTo 的实现(我在这里使用 temurin)
@Override
public int compareTo(UUID val) {
// The ordering is intentionally set up so that the UUIDs
// can simply be numerically compared as two numbers
int mostSigBits = Long.compare(this.mostSigBits, val.mostSigBits);
return mostSigBits != 0 ? mostSigBits : Long.compare(this.leastSigBits, val.leastSigBits);
}
对于这种特殊情况,最高有效位是
-5389922481480318956
1377831944219938421
分别表示。这意味着比较是错误的,因为 long 溢出了。
是的。报告的错误是JDK-7025832 - “
java.lang.UUID compareTo()
不进行无符号比较”。它已被标记为“不会修复”,因为跨 Java 版本的行为保持一致非常重要。
compareTo
Comparator
如果您特别想要这种行为,那么编写一个以无符号方式比较它们的函数很简单。只需使用Long.compareUnsigned
。