简体   繁体   English

为什么list.sort不使用Optional API

[英]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. 在此上下文中, nullOptional.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.

相关问题 "Java stream().sorted 或 list.sort() 不会增加时间复杂度吗?" - Does Java stream().sorted or list.sort() not increases time complexity? Java 8 List.sort用法 - Java 8 List.sort usage List.sort 有两个字段 - List.sort with two fields 为什么 Collections.sort 可以不带比较器,但 List.sort 必须带比较器? - Why can Collections.sort take no comparator but List.sort must take a comparator? List.sort()NoSuchMethodException 1.6 vs 1.8 - List.sort() NoSuchMethodException 1.6 vs 1.8 Collections.sort(list) 和 list.sort(Comparator) 的区别 - Difference between Collections.sort(list) and list.sort(Comparator) 尝试使用 Map.values().parallelStream().forEach(list -&gt; list.sort(comparator)) 但出现错误:“比较方法违反其一般合同!” - Trying to use Map.values().parallelStream().forEach(list -> list.sort(comparator)) but get error: "Comparison method violates its general contract!" 排除一个特定值时list.sort()如何工作? - how list.sort() works when exclude one specific value? 为什么不对列表进行排序? - Why does this not sort the list? 您可以实现并重写list.sort()方法来对有理数列表进行排序吗? - Can you implement and override the list.sort() method to sort a list of rational numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM