13

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?

Martin Schröder
  • 354
  • 1
  • 4
  • 19
InformedA
  • 2,991
  • 2
  • 19
  • 28
  • 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](http://programmers.stackexchange.com/a/332489/16247), however... ugh :| – Andres F. Oct 01 '16 at 02: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 01 '16 at 02: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 01 '16 at 02:54

1 Answers1

13

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.

amon
  • 132,749
  • 27
  • 279
  • 375
  • Thanks for the quick answer. This just one thing which confirms to me that lambda expression and stream are just bullshit under the hood. I would rather just use anonymous inner class and/orvisitor pattern in object oriented programming style than obfuscating my head with twisted saleman pundit of "concise and easier to read" – InformedA Sep 30 '16 at 20:38
  • 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
  • I don't say they are "useless". I meant to say that they are just convenient features which unnecessarily causes more learning curve while adding very little. They pose the danger for people who do not understand things under the hood taking them as a gain of more than "reading and writing" convenience. They do not have additional gain than very little "reading and writing" convenience. – InformedA Sep 30 '16 at 21:00
  • 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...](https://www.amazon.com/Mastering-Lambdas-Programming-Multicore-Oracle/dp/0071829628/ref=sr_1_1?ie=UTF8&qid=1475273330&sr=8-1&keywords=naftalin+lambda) 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
  • @YuriSteinschreiber Isn't it true that multicore parallel from stream is the same as using an ExecutorService and submitting multiple tasks, each task is a call to the lambda expression? – InformedA Oct 01 '16 at 01:01
  • 1
    No. What you said is the low-level part, question is how you split the workload and merge the results. And another question is how you compose different operations on streams without introducing too much overhead. Java streams provide mechanisms for answering both these questions, in a quite non-trivial way. That sets Java streams apart from many similar APIs in other languages, most notably C#. That besides the fact that a stream abstraction is something worth learning by itself as @JörgWMittag mentions. – Yuri Steinschreiber Oct 01 '16 at 01:10
  • @YuriSteinschreiber If your answer is no, then how is parallel run of Stream implemented? Can you describe in plain English? I feel that it is the same as using ExecutorService and it severely stops me from considering it as anything more than convenient feature. – InformedA Oct 01 '16 at 01:20
  • I suggested to read a book. Or you can just browse JRE code. We are not here to force you to learn, in plain English or otherwise. If you want to stick to your preconceived notions as @amon suggested and don't want to use anything beyond your understanding - fine. – Yuri Steinschreiber Oct 01 '16 at 01:26
  • @YuriSteinschreiber Actually, I want to seriously consider it. That is why I spent my time asking question such as this one. But there is a problem with my effort. That is I don't see much gain from it other than little "reading and writing" convenience. In other words, I am just looking for evidences to make a decision about considering lambda and stream. – InformedA Oct 01 '16 at 01:34
  • 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 01 '16 at 02:33
  • @AndresF. Those examples of yours for high level features of programming languages provide **significant** if not massive convenience. I am more than happy and consider them. Lambdas and stream on the other hands, I can't really see any significant gain. – InformedA Oct 01 '16 at 02:59
  • @InformedA In my opinion, that's simply lack of experience. Have you tried functional languages? Lambdas and streams are essential for them -- they provide the "massive convenience" that you mentioned. Some problems become conceptually easier or more elegant with lazy lists / enumerators / whatever you wish to call them. This is simply a case of Java adopting widespread functional idioms. – Andres F. Oct 01 '16 at 03:04
  • 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 01 '16 at 03:10