0

How do elements of a stream go thought the stream itself? Is it like it takes 1 element and passes it thought all functions (map then sort then collect) and then takes second elements and repeats the cycle or is it like it takes all elements and maps them then sorts and finally collects?

new ArrayList<Integer>().stream()
                .map(x -> x.byteValue())
                .sorted()
                .collect(Collectors.toList()); 
  • 2
    How could `sorted()` only take one by one and directly passes it through? – Seelenvirtuose May 20 '20 at 07:49
  • 1
    Does this answer your question? [Do sorted and distinct immediately process the stream?](https://stackoverflow.com/questions/49289094/do-sorted-and-distinct-immediately-process-the-stream) – Eklavya May 20 '20 at 07:56

4 Answers4

1

That depends on the actual Streamimplementation. This mostly applies to parallel streams, because spliterators tend to chunk the amount of data and you don't know which element will be process when.

In general, a stream goes through each element in order (but doesn't have to). The simplest way to check this behaviour is to put in some breakpoints and see when they actually hit.

Also, certain operations may wait until all prior operations are executed (namely collet())

I advise to check the javadoc and read it carefully, because it gives away enough hints to get an expectation.

Isfirs
  • 124
  • 1
  • 12
1

It depends entirely on the stream. It is usually evaluated lazily, which means it takes it one element at a time, but under certain conditions it needs to get all the elements before it continues to the next step. For example, consider the following code:

IntStream.generate(() -> (int) (Math.random() * 100))
    .limit(20)
    .filter(i -> i % 2 == 0)
    .sorted()
    .forEach(System.out::println);

This stream generates random numbers from 0 to 99, limited to 20 elements, after which it filters the numbers by checking wether or not they are even, if they are, they continue. Until now, it's done one element at a time. The change comes when you request a sorting of the stream. The sorted() method sorts the stream by the natural ordering of the elements, or by a provided comparator. For you to sort something you need access to all elements, because you don't know the last element's value until you get it. It could be the first element after you sort it. So this method waits for the entire stream, sorts it and returns the sorted stream. After that this code just prints the sorted stream one element at a time.

Ivan Dimitrov
  • 332
  • 1
  • 10
1

something like this, yes.

if you have a stream of integers let's say 1,2,3,4,5 and you do some operations on it, let's say stream().map(x -> x*3).filter(x -> x%2==0).findFirst()

it will first take the first value (1), it will be multiplied by 3, and then it will check if it's even.

Because it's not, it will take the second one (2), multiply by 3 (=6), check if it is even (it is), find first.

this will be the first one and now it stops and returns.

Which means the other integers from the stream won't be evaluated (multiplied and checked if even) as it is not necessary

Emanuel Trandafir
  • 1,526
  • 1
  • 5
  • 13
0

If you are using IntelliJ, they have this visualization feature that can be really useful when you start working with streams. You have to stop on a breakpoint and then click on the Trace Current Stream Chain button.

enter image description here

more about this feature in this blog post.

Emanuel Trandafir
  • 1,526
  • 1
  • 5
  • 13