2

What is the best way to implement a result limit when collecting data via Java 8 parallel streams?

objects.parallelStream().forEach((object) -> {
  ... complex logic here ...
  if (allGood) {
    someCollection.add(someData);
    someMap.putAll(someEntries);
  }
}

What is the best way to halt all further processing once the size of 'someCollection' has reached a result limit?

Jess Holle
  • 633
  • 5
  • 15
  • 1
    http://stackoverflow.com/questions/20746429/java-8-limit-infinite-stream-by-a-predicate – assylias Jun 05 '15 at 12:54
  • 2
    Not sure, but how about `parallelStream() .filter(functionToDetermineAllGood) .limit(number).forEach(...)`? – tobias_k Jun 05 '15 at 12:55
  • 2
    There is no good way. Limits and parallel execution don’t play well together. It all depends on the question, which compromise you are willing to accept. – Holger Jun 05 '15 at 13:03

0 Answers0