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?
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.
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.
Java Stream sorted() Learn to use Stream sorted() method to sort the elements in a Stream by their natural order.
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.
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.
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.
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
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With