Shortcuts on Collections to create Streams

Zs. gzsombor at
Wed Mar 3 17:16:25 UTC 2021


 As Collection already has a default implementation for 'forEach', I'm
wondering why there is no similar shortcuts for a couple of repeating
patterns around the Collection <--> Stream transitions?
For example, if you could write: instead of;
 collection.filter(...) instead of;
Or for the other direction:
 stream.toList() instead of stream.collect(Collectors.toList())
 stream.toSet() instead of stream.collect(Collectors.toSet())

They are heavily used even in OpenJDK's source code:

   -  380 results, unfortunately with false matches from
      - 559 results, unfortunately with false matches from comments or
      streams().mapToInt or not related code

   -  367 results
   -  117 results

Any particular reason, why adding these - 4 or more - default method to a
collection deemed unnecessary or harmful? Unfortunately I haven't been able
to find the answers for this question using internet searches.


More information about the jdk-dev mailing list