Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream sorted() leads to unexpected results

I have an Array and a Stream like below.

String[] names = {"Alex", "Anna", "Abhi", "Some", "Broad", "Dustin", 
                  "amanda", "Hanna", "Steve", "Sydney"};
        
Stream.of(names)
        .sorted()
        .map(String::toLowerCase)
        .filter(x -> x.startsWith("a"))
        .forEachOrdered(System.out::println);
        //I have also tried - .forEach(System.out::println);

Actual Output:

abhi
alex
anna
amanda

Expected Output:

abhi
alex
amanda
anna

What am I missing here?

like image 700
Ajay Kumar Avatar asked Jan 19 '21 19:01

Ajay Kumar


People also ask

Is stream sorted stable?

Stream sorted() in JavaFor ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.

What is the purpose of sorted method of stream in Java 8?

Found within the Stream interface, the sorted() method has two overloaded variations that we'll be looking into. This methods returns a stream consisting of the elements of the stream, sorted according to natural order - the ordering provided by the JVM. If the elements of the stream are not Comparable , a java.

Which method is used to sort the stream?

Java Stream sorted() Learn to use Stream sorted() method to sort the elements in a Stream by their natural order.

What is Strem in Java?

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are – A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.

What is the difference between sorted and unordered streams?

Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.

What is sorted stream in Java?

Stream.sorted () This is a stateful intermediate operation which returns a new stream. Returns a stream consisting of the elements of this stream, sorted according to natural order. If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

How to sort elements in a stream by their natural order?

Learn to use Stream sorted () method to sort the elements in a Stream by their natural order. We can also sort the elements using the provided Comparator. 1. Stream sort () Method

Why do I always get an unexpected end of stream?

So there are many reasons you could get an unexpected end of stream; it is unfortunate that most programs have rather simplistic responses, instead of giving the kind of detailed message that would help diagnose what is wrong.


2 Answers

You are first sorting and only then applying the toLowerCase on the elements, and since Anna has capital A will come before amanda.

Do the following instead:

Stream.of(names)
        .map(String::toLowerCase)
        .filter(x -> x.startsWith("a"))
        .sorted()
        .forEachOrdered(System.out::println);

Output:

abhi
alex
amanda
anna
like image 62
dreamcrash Avatar answered Oct 28 '22 15:10

dreamcrash


You are missing the .sorted() call before forEachOrdered, otherwise the contents of the stream will not be sorted after applying .toLowerCase().

What forEachOrdered does is run a function on every item in the stream in the order that they are in the stream; it does not sort the values themselves.

like image 5
Billy Brown Avatar answered Oct 28 '22 16:10

Billy Brown