By tkruse

2019-06-10 15:59:09 8 Comments

There is the question on whether java methods should return Collections or Streams, in which Brian Goetz answers that even for finite sequences, Streams should usually be preferred.

But it seems to me that currently many operations on Streams that come from other places cannot be safely performed, and defensive code guards are not possible because Streams do not reveal if they are infinite or unordered.

If parallel was a problem to the operations I want to perform on a Stream(), I can call isParallel() to check or sequential to make sure computation is in parallel (if i remember to).

But if orderedness or finity was relevant to the safety of my program, I cannot write safeguards.

Assuming I consume a library implementing this fictitious interface:

public interface CoordinateServer {
    public Stream<Integer> coordinates();
    // example implementations:
    // IntStream.range(0, 100).boxed()   // finite, ordered, sequential
    // final AtomicInteger atomic = new AtomicInteger();
    // Stream.generate(() -> atomic2.incrementAndGet()) // infinite, unordered, sequential
    // Stream.generate(() -> atomic2.incrementAndGet()).parallel() // infinite, unordered, parallel

Then what operations can I safely call on this stream to write a correct algorithm?

It seems if I maybe want to do write the elements to a file as a side-effect, I need to be concerned about the stream being parallel:

// if stream is parallel, which order will be written to file?
coordinates().peek(i -> {writeToFile(i)}).count();
// how should I remember to always add sequential() in  such cases?

And also if it is parallel, based on what Threadpool is it parallel?

If I want to sort the stream (or other non-short-circuit operations), I somehow need to be cautious about it being infinite:

coordinates().sorted().limit(1000).collect(toList()); // will this terminate?
coordinates().allMatch(x -> x > 0); // will this terminate?

I can impose a limit before sorting, but which magic number should that be, if I expect a finite stream of unknown size?

Finally maybe I want to compute in parallel to save time and then collect the result:

// will result list maintain the same order as sequential?
coordinates().map(i -> complexLookup(i)).parallel().collect(toList());

But if the stream is not ordered (in that version of the library), then the result might become mangled due to the parallel processing. But how can I guard against this, other than not using parallel (which defeats the performance purpose)?

Collections are explicit about being finite or infinite, about having an order or not, and they do not carry the processing mode or threadpools with them. Those seem like valuable properties for APIs.


Related Questions

Sponsored Content

58 Answered Questions

[SOLVED] How can I concatenate two arrays in Java?

18 Answered Questions

[SOLVED] What's the difference between map and flatMap methods in Java 8?

9 Answered Questions

[SOLVED] How to Convert a Java 8 Stream to an Array?

1 Answered Questions

[SOLVED] stateful and stateless methods of stream

20 Answered Questions

[SOLVED] Java: when to use static methods

8 Answered Questions

[SOLVED] Convert Iterable to Stream using Java 8 JDK

3 Answered Questions

2 Answered Questions

[SOLVED] Will this make a faster parallel stream?

3 Answered Questions

[SOLVED] Is it worth using distinct() with collect(toSet())

  • 2017-01-11 14:31:29
  • Neil Madden
  • 3212 View
  • 11 Score
  • 3 Answer
  • Tags:   java java-stream

1 Answered Questions

Sponsored Content