简体   繁体   中英

Why Java 8 Stream interface does not have min() no-parameter version?

java.util.stream.Stream interface has two versions of sorted method – sorted() which sorts elements in natural order and sorted(Comparator) . Why min() method was not introduced to Stream interface, which would return minimal element from natural-ordering point of view?

It should be clear that for min , max , and sorted , adding a method to Stream that does not require a comparator introduces a way to lose the generic type safety. The reason is that the current version of the Java language does not support restricting methods to instances of a specific parameterization, ie limit them to streams of comparable elements.

So the question could be the other way round, why has this potential break of the type safety been allowed with sorted() ?

I can't look into the developers mind, but one interesting point is that sorting has been treated specially for a long time now. With the introduction of Generics, it became possible to enforce that sorting without a Comparator can only be attempted for collections or arrays with comparable elements. However, especially when implementing generic collections, developers might face the fact that arrays can't be created with a generic element type. There might be other scenarios, where a developer encounters an array or collection of a formally non-comparable type while the contained elements are comparable for sure. As said, I can't look into the developers mind to say, which scenarios were considered.

But

But it's not just all about backwards compatibility. List.sort(Comparator) , introduced in Java 8, is also specified as accepting null as argument for “natural order”, so now we have another scenario, where an implementer might have to sort data without a compile-time type that guarantees comparable elements.

So when it comes to sorting, there are already lots of opportunities to dodge the type system. But Stream.sorted(Comparator) is the only sort method not accepting a null comparator. So sorting by natural order without specifying Comparator.naturalOrder() is only possible using sorted() without arguments. By the way, having an already sorted input with a null comparator and requesting sorted() without comparator is the only situation where the Stream implementation will detect that sorting isn't necessary, ie it doesn't compare comparators and doesn't check for Comparator.naturalOrder() .

Generally, the type safety of comparators is astonishing weak. Eg Collections.reverseOrder() returns a comparator of arbitrary type, not demanding the type to be comparable. So instead of min() , you could use max(Collections.reverseOrder()) to request the minimum, regardless of the stream's formal type. Or use Collections.reverseOrder(Collections.reverseOrder()) to get the equivalent of Comparator.naturalOrder() for an arbitrary type. Likewise, Collator implements Comparator<Object> , for whatever reason, despite it can only compare String s.

I'd assume that would just pollute the API. one could say "why is there no max parameterless version" , "why is there no CharStream" and it could on and on in terms of the things that could be made available but were decided best not to.

having a parameterless min method over this:

someList.stream().min(Comparator.naturalOrder());

would be no different.

therefore it's best to just create a reusable method rather than polluting the API with all the possible things.

I think min() only allows the signature that accepts a Comparator because the Stream could be of any type, even a type created by you. In such case it would be impossible to rely on a natural order, as the class you've created, can't have a natural order until you specify it.

If, insteam of the class Stream you use, IntStream, you'll see that a min() method with no arguments is defined. This does what you want. It is the following:

public static void main (String... args){
        IntStream s=IntStream.of(1,2,3,4,5,6,7,8,9,10);
        System.out.println(s.min().getAsInt());
    }

Comparator.naturalOrder serves this purpose. Its existence (or custom implementations thereof) allows other classes to remain simpler because they don't have to implement special codepaths for null-values in Comparator fields.

Its type will also force the stream's T to implement Comparable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM