[英]Why list.sort does not use the Optional API
Java 8 introduces a new default method on the List
interface to sort it. Java 8在
List
接口上引入了一个新的默认方法来对其进行排序。 It's signature is : 它的签名是:
void sort(Comparator<? super E> c)
The documentation says: 文件说:
If the specified comparator is null then all elements in this list must implement the Comparable interface and the elements' natural ordering should be used.
如果指定的比较器为null,则此列表中的所有元素都必须实现Comparable接口,并且应使用元素的自然顺序。
So if you want to sort the list by it's natural order (and that your elements are comparable) you have to do list.sort(null);
因此,如果您想按照它的自然顺序对列表进行排序(并且您的元素具有可比性),则必须执行
list.sort(null);
which is kind of weird of my opinion. 这有点奇怪我的意见。
If they used an Optional
the doc would stated that you can optionally provide a comparator, and that if it's not provided it will assume the elements are already comparable. 如果他们使用了
Optional
那么doc会说你可以选择提供一个比较器,如果没有提供,它会认为这些元素已经具有可比性。
A list.sort(null);
list.sort(null);
call would be transformed into list.sort(Optional.empty());
call将被转换为
list.sort(Optional.empty());
. 。
As it's a method that it exposed to the outside world, I would find it more accurate. 因为它是一种暴露于外部世界的方法,我会发现它更准确。
Why didn't they used the new Optional API instead? 他们为什么不使用新的Optional API呢?
Optional is intended to be used as a return type. 可选用作返回类型。 That has always been the mantra of the JDK-8 developers.
这一直是JDK-8开发人员的口头禅。 So they won't break their own rule by using it as an argument.
因此,他们不会通过使用它作为论据来打破自己的规则。
That said, I would have made the argument mandatory, and thus force the developer to use 也就是说,我会强制论证,从而迫使开发人员使用
list.sort(Comparator.<Foo>naturalOrder());
Even if I can pass null, I find the above more readable, and not much more verbose. 即使我可以传递null,我发现上面的内容更具可读性,而且更加冗长。 So that's what I use in my code.
这就是我在代码中使用的内容。
The default method is delegating to Arrays#sort
, which has existed since at least Java 1.7 . 默认方法是委托给
Arrays#sort
,它至少从 Java 1.7开始存在 。
Here's the relevant snippet for the default method: 这是默认方法的相关代码段:
@SuppressWarnings({"unchecked", "rawtypes"})
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
Observe that it's converting the list into an array and letting Arrays#sort
handle it from there. 观察它正在将列表转换为数组并让
Arrays#sort
从那里处理它。 The default behavior for this then would fall back to what is supported for that method. 然后,此默认行为将回退到该方法支持的行为。
There are two reasons why I see this being preferable to adding an Optional
: 有两个原因让我觉得这比添加一个
Optional
更可取:
If you don't have a Comparator
to use, or just want the "default" behavior, you can provide a null
to it. 如果你没有一个
Comparator
使用,或只是想“默认”的行为,你可以提供一个null
吧。 In this context, null
and Optional.isPresent()
serve the same purpose and would not earn any usability points. 在此上下文中,
null
和Optional.isPresent()
用于相同的目的,不会获得任何可用性点。
It is an annoyance to have to provide null
to a function for its default behavior; 必须为函数的默认行为提供
null
是一件烦恼; better design might have been to either overload the method or allow a default naturalOrder
instance to be passed in instead. 更好的设计可能是重载方法或允许传入默认的
naturalOrder
实例。
The Optional
pattern is more meant to guard against inadvertently handling a null
reference, as opposed to being used for a null
check. Optional
模式更倾向于防止无意中处理null
引用,而不是用于null
检查。 The overhead in adding Optional
where a simple null
check would suffice would far outweigh its benefits, especially considering that there is no semantic difference. 添加
Optional
的开销在一个简单的null
检查就足够了将远远超过它的好处,特别是考虑到没有语义差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.