0

I don't know why I should use aggregate functions. I mean, it is supposed that an aggregate function would parallelize the execution if improves the performance.

https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

But it is not true, according to the documentation, the code won't be parallel if you do not use a parallelStream() instead stream(), so Why should I use a stream() if nothing goes better?

Shouldn't those codes be the same?

//it is not parallel
listOfIntegers.stream()
            .forEach( e -> System.out.print(e+" "));

And

//it is parallel
listOfIntegers.parallelStream()
            .forEach( e -> System.out.print(e+" "));
Cœur
  • 37,241
  • 25
  • 195
  • 267
David Marciel
  • 865
  • 1
  • 12
  • 29
  • 1
    What do you mean with "aggregate" functions? Usage of streams? – J. Dow May 04 '16 at 14:35
  • 1
    You are using lambda-expressions in your Statements. Do you mean map, filter, reduce as aggregate-functions? – Supahupe May 04 '16 at 14:39
  • 1
    `forEach` is not an aggregate function. Besides that, in your example you most likely want to use `stream()` instead of `parallelStream()` because the sequential `stream()` will be faster in most cases. – Holger May 04 '16 at 15:18
  • @Holger, `forEach()` is an aggregate operation, at least in some sense, because it is referred to as such in the tutorial. See https://docs.oracle.com/javase/tutorial/collections/streams/index.html – Hank D May 04 '16 at 16:15
  • Yes, Java call the operations in a stream "aggregate functions", https://docs.oracle.com/javase/tutorial/collections/streams/index.html – David Marciel May 05 '16 at 07:24
  • The question for me is: why should I use aggregate functions if they don't improve the performance by parallelizing the load?, I understand them as a way to do so, It is an unordered stream that perform operations in a collection. I understand it means it will parallelize the work, but it is not parallel unless you say so explicit. – David Marciel May 05 '16 at 07:27

1 Answers1

2

if you use stream, all data in your list will be processed in order, while if you use parallelStream your data might not be process in order.

consider method

static void test(Integer i){
        try {
            Thread.sleep((long) (1000*Math.random()));
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(i);
    }

and compare output from this method using parallelStream and stream

user902383
  • 8,420
  • 8
  • 43
  • 63
  • The fact that `List.Stream()` processes data in order says more about the internals of the `List` than the behavior of `stream()`. A `Set`, for example, feeds a stream in no particular order. `forEach()`, by definition, makes the stream unordered, and so one should never count on the implementation detail of `List` behavior to expect that elements would be processed in order. One should use `forEachOrdered`, not `forEach` to intentionally keep things ordered, and that will work for both `stream()` and `parallelStream()` – Hank D May 04 '16 at 16:26