When I call Stream.sort(..) is there a new array of elements created and the stream iterates over the newly created sorted array?

In other words, how Java 8 Stream does sort under the hood?

  • Why do I get a down vote with this question??? – InformedA Sep 30 '16 at 20:41
  • 2
    Your question is reasonable and undeserving of downvotes. Your comments on amon's answer, however... ugh :| – Andres F. Oct 1 '16 at 2:36
  • @AndresF. The down vote came even before I made that comment. It is one of the reason why I was very upset. – InformedA Oct 1 '16 at 2:52
  • The comments are no reason to downvote, anyway. The question stands on its own merit, and it's valid in my opinion. I upvoted it. – Andres F. Oct 1 '16 at 2:54

You can use grepcode.com to search through the Java standard library code (and some other libraries). Unfortunately, the stream implementation code is rather abstract. A good starting point is the internal java.util.stream.SortedOps class which transforms a stream into a sorted stream.

The current implementation (used for streams of standard library containers) makes it a no-op if the stream is already sorted, uses an array if the size of the stream is known (SizedRefSortingSink), or accumulates all elements in an ArrayList if the size is unknown (RefSortingSink).

Of course, such implementation details may change with any release, but the fundamental considerations are universal: Sorting a stream is necessarily an eager/blocking operation, and sorting an infinite stream is not meaningful. This means sorting a stream is not useful if you use streams because they can be lazy, but you still get the convenient stream syntax.

Other streams will have to provide their own implementation of Stream.sorted(), which will likely be similar.

  • 1
    @InformedA I do not want to suggest that lambdas or streams would be “bullshit under the hood”. They are both incredibly convenient, even though the details regarding streams are unusually complex compared to other Java concepts. If you want to stick to your pre-conceived notion that these tools are useless or harmful, you are unnecessarily limiting yourself. – amon Sep 30 '16 at 20:53
  • 1
    @amon - agreed, plus streams provide a possibility of rolling multicore parallel implementations under the hood, without virtually changing the application. And the complexity of stream implementation comes exactly from that. That's way more than just convenience, that's right abstraction. To the OP - I suggest you read Mastering Lambdas... if you want to understand why lambdas and streams are so much more that just convenient features. – Yuri Steinschreiber Sep 30 '16 at 22:19
  • 3
    @InformedA: lambdas have been around for 80 years and exist in pretty much every current mainstream programming language. Streams have been around for 40 years, and likewise exist in pretty much every mainstream collections framework. They might be called different things (iterators, lazy lists, enumerators, enumerables), but they're there. Lambdas and lazy lists are some of the oldest and most stable abstractions there are, and they have survived every new fad, hype, paradigm, movement, methodology, technology, language, OS, framework, library thrown at them. That makes them worth a look. – Jörg W Mittag Sep 30 '16 at 22:48
  • 3
    @InformedA Java, the programming language, is just a bullshit abstraction of bytecode running on the JVM. The JVM itself is just a bullshit abstraction written in C (or C++, I forget). C and C++ are just bullshit abstractions over assembly language. Even assembly language itself is a bullshit abstraction over microcode, which is also a bullshit abstraction over circuits (ok, I might be missing a few steps in between). You could say everything useful in software is a "bullshit abstraction" over something else. – Andres F. Oct 1 '16 at 2:33
  • 3
    @InformedA My honest advice is that you try learning a language that is more oriented to functional programming than Java. Even if you never use it for your day job, you'll gain an understanding of programming languages and their design choices that will help you with Java :) – Andres F. Oct 1 '16 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.