4

from this question

a spliterator reporting either, IMMUTABLE or CONCURRENT, is guaranteed to never throw a ConcurrentModificationException. Of course, CONCURRENT precludes SIZED semantically, but that has no consequence to the client code.

In fact, these characteristics are not used for anything in the Stream API, hence, using them inconsistently would never get noticed somewhere.

This is also the explanation why every intermediate operation has the effect of clearing the CONCURRENT, IMMUTABLE and NONNULL characteristics: the Stream implementation doesn’t use them and its internal classes representing the stream state do not maintain them.

if stream doesnt use CHARACTERISTICS from source then how stream works parallely? does stream completely ignores the stream source characteristics?

from this question Collector does not know that im using a concurrent collection provided by Supplier, so characteristics are not infered from type of collector container

  1. under what circumstances stream API considers characteristics ?
  2. which operation resets which characteristics ?
amarnath harish
  • 945
  • 7
  • 24
  • 5
    The implementation does not use _these_ characteristics. Other characteristics are used like `SIZED` and `SUBSIZED`. – Didier L Sep 17 '18 at 14:40
  • why they don't use ? so if I call sorted() on sorted stream its gonna run again and sort? if stream doesn't use characteristics of `spliterator` who else is gonna use them. I personally don't see any reason to use `spliterator` to split a collection and manually optimize it to run on parallel. – amarnath harish Sep 17 '18 at 14:43
  • so spliiterator behaves based on its characteristics and these characteristics are forgotten in streams API ? – amarnath harish Sep 17 '18 at 14:45
  • 8
    The cited statement is specifically talking about `CONCURRENT`, `IMMUTABLE`, and `NONNULL`. In contrast, `ORDERED`, `DISTINCT`, `SORTED`, `SIZED`, and `SUBSIZED` *are* used. And currently unused characteristics might be used in the future. – Holger Sep 17 '18 at 15:30
  • 3
    "So, if I call sorted() on sorted stream its gonna run again and sort?" AFAIK, if the spliterator is naturally `SORTED` (the associated `Comparator` is `null`) then the `SORTED` characteristic is accounted for in the Stream flags. – Stefan Zobel Sep 17 '18 at 15:30

1 Answers1

6

What you are asking for is possible. The correct verbiage in those answers, would be that at the moment those properties are ignored, in some future they might be injected/read/used by stream implementation.

Also in your comments, you say that:

someTreeSet().stream()
         .sorted()
         .... some other operations 

Will call sorted. This is simply not true, in this case, that operation will not get called. This is one stream flag that is not ignored and injected into the stream implemntation by TreeSet.

Eugene
  • 117,005
  • 15
  • 201
  • 306