Objects.compare() 方法的目的是什么?
What is the purpose of the Objects.compare() method?
Java 7介绍了Objects
class containing “null
-safe or null
-tolerant” methods, including compare(T, T, Comparator<T>)
。但是我什么时候会使用
Objects.compare(left, right, comparator);
简单调用
comparator.compare(left, right);
?
Objects.compare
只有 null
安全,如果 comparator
也是安全的,那我为什么要包装比较调用?首先检查对象身份的优化似乎应该在比较器本身中完成。我能看到的唯一真正的行为差异是,如果 comparator
、left
和 right
都是 null
,则 comparator.compare(left, right)
会抛出 NullPointerException
,而 Objects.compare
没有;这似乎并不是一个足够重要的考虑因素来保证一个新的标准库方法。
我是不是漏掉了什么明显的东西?
这很有趣。 Objects
class 以及此 .compare()
方法在 3b45b809d8ff by Joe Darcy, and the commit message cites Bug 6797535 and indicates "Sherman" (Xueming Shen?) signed off on it. In addition to the bug there is this thread from Sept. 2009 discussing what features to add to Objects
. In the thread folks discuss adding compare(int, int)
(and the like) primitive comparison methods, and eventually decide these should reside in their respective wrapper classes (see Integer.compare()
). This method is introduced later in that thread 中介绍,但没有我能找到的任何评论。
稍后 a patch is sent out for review containing Objects.compare()
, and Joshua Bloch replies:
I don't think you should add this method ( compare(T a, T b, Comparator c)). Its utility is unclear, and it doesn't have the
power-to-weight ratio of the other methods in this class.
Yeah, I included this with "Item 12: Consider implementing Comparable"
from EJv2 in mind.
我不清楚这种方法给 table 关于 Item 12 带来了什么,但这个问题似乎没有再次提出。我推断,出于风格原因,其目的是提供一种等同于原始 compare()
的方法,但我还没有找到实际原因的证据。
值得注意的是,在达西和布洛赫的同一次交流中,Objects.toString()
方法同样受到布洛赫的批评:
I would definitely /not/ add this method (Objects.toString). It brings nothing to the table that isn't already there. People know and use String.valueOf. Let's not muddy the waters by adding another choice.
但据我们所知,它并没有被删除,达西只是回应道:
So noted.
所以总而言之,在我看来,这似乎是在没有太多意图的情况下引入的。它被提议并且提出的反对意见并没有阻止它被签入。我认为更严格的设计审查会犯错误,而不是像布洛赫建议的那样将其排除在外。
您可能也对这个类似的问题感兴趣(尽管它是关于实现细节,而不是 API 更改):
Java 7介绍了Objects
class containing “null
-safe or null
-tolerant” methods, including compare(T, T, Comparator<T>)
。但是我什么时候会使用
Objects.compare(left, right, comparator);
简单调用
comparator.compare(left, right);
?
Objects.compare
只有 null
安全,如果 comparator
也是安全的,那我为什么要包装比较调用?首先检查对象身份的优化似乎应该在比较器本身中完成。我能看到的唯一真正的行为差异是,如果 comparator
、left
和 right
都是 null
,则 comparator.compare(left, right)
会抛出 NullPointerException
,而 Objects.compare
没有;这似乎并不是一个足够重要的考虑因素来保证一个新的标准库方法。
我是不是漏掉了什么明显的东西?
这很有趣。 Objects
class 以及此 .compare()
方法在 3b45b809d8ff by Joe Darcy, and the commit message cites Bug 6797535 and indicates "Sherman" (Xueming Shen?) signed off on it. In addition to the bug there is this thread from Sept. 2009 discussing what features to add to Objects
. In the thread folks discuss adding compare(int, int)
(and the like) primitive comparison methods, and eventually decide these should reside in their respective wrapper classes (see Integer.compare()
). This method is introduced later in that thread 中介绍,但没有我能找到的任何评论。
稍后 a patch is sent out for review containing Objects.compare()
, and Joshua Bloch replies:
I don't think you should add this method ( compare(T a, T b, Comparator c)). Its utility is unclear, and it doesn't have the power-to-weight ratio of the other methods in this class.
Yeah, I included this with "Item 12: Consider implementing Comparable" from EJv2 in mind.
我不清楚这种方法给 table 关于 Item 12 带来了什么,但这个问题似乎没有再次提出。我推断,出于风格原因,其目的是提供一种等同于原始 compare()
的方法,但我还没有找到实际原因的证据。
值得注意的是,在达西和布洛赫的同一次交流中,Objects.toString()
方法同样受到布洛赫的批评:
I would definitely /not/ add this method (Objects.toString). It brings nothing to the table that isn't already there. People know and use String.valueOf. Let's not muddy the waters by adding another choice.
但据我们所知,它并没有被删除,达西只是回应道:
So noted.
所以总而言之,在我看来,这似乎是在没有太多意图的情况下引入的。它被提议并且提出的反对意见并没有阻止它被签入。我认为更严格的设计审查会犯错误,而不是像布洛赫建议的那样将其排除在外。
您可能也对这个类似的问题感兴趣(尽管它是关于实现细节,而不是 API 更改):