Shortcuts on Collections to create Streams
Andreas Aronsson
aron at aron.nu
Fri Mar 5 05:07:28 UTC 2021
All Brian's points are valid.
I am really happy about this addition
https://download.java.net/java/early_access/jdk16/docs/api/java.base/java/util/stream/Stream.html#toList()
It is really very useful to me.
Thanks to Zs for raising this.
On Thu, Mar 4, 2021 at 7:54 PM Brian Goetz <brian.goetz at oracle.com> wrote:
> We are extremely conservative about adding "convenience" methods; in
> general, they don't carry their weight. There are exceptions, but these
> have to be justified carefully.
>
> Your original mail illustrates one of the many problems with convenience
> methods: every one we add leads to a call for more:
>
> > 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?
>
> But a world with the union of all possible convenience methods that
> someone could imagine being useful, is not the world we want to live in;
> both library implementors and users would be overwhelmed with trivial
> differences, and this crowds out higher-value activities. In general, we
> prefer to expose the primitives, and let users combine them, though
> different JDK libraries follow this to different degrees. But, in
> general, a "trivial" method requires a nontrivial amount of justification.
>
> On 3/4/2021 1:56 AM, Zs. wrote:
> > Thanks, so I'm late by about 4 months for that method.
> > I've found the tickets about this for future reference:
> > https://bugs.openjdk.java.net/browse/JDK-8180352
> > and the pull request - https://github.com/openjdk/jdk/pull/1026
> > Now, I only miss the 3 other what I've mentioned :)
> >
> > Best regards,
> > Zsombor
> >
> > On Wed, Mar 3, 2021 at 10:53 PM Thiago Henrique Hupner <thihup at gmail.com
> >
> > wrote:
> >
> >> They have added the Stream#toList in JDK 16:
> >>
> https://download.java.net/java/early_access/jdk16/docs/api/java.base/java/util/stream/Stream.html#toList()
> >>
> >>
> >> Em qua., 3 de mar. de 2021 às 18:45, Zs. <gzsombor at gmail.com> escreveu:
> >>
> >>> Hi,
> >>>
> >>> 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:
> >>> collection.map(...) instead of collection.stream().map(...);
> >>> collection.filter(...) instead of collection.stream().filter(...);
> >>> 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:
> >>>
> >>> -
> >>>
> >>>
> https://github.com/search?q=stream%5C%28%5C%29.filter%28+repo%3Aopenjdk%2Fjdk+language%3AJava&type=Code&ref=advsearch&l=Java&l=
> >>> - 380 results, unfortunately with false matches from
> javax.xml.stream.
> >>> StreamFilter
> >>> -
> >>>
> >>>
> https://github.com/search?q=stream%5C%28%5C%29.map%5C%28%5C%29+repo%3Aopenjdk%2Fjdk&type=Code
> >>> - 559 results, unfortunately with false matches from comments or
> >>> streams().mapToInt or not related code
> >>>
> >>>
> >>> -
> >>>
> >>>
> https://github.com/openjdk/jdk/search?q=collect%28Collectors.toList%28%29%29
> >>> - 367 results
> >>> -
> >>>
> >>>
> https://github.com/openjdk/jdk/search?q=collect%28Collectors.toSet%28%29%29
> >>> - 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.
> >>>
> >>> Thanks,
> >>> Zsombor
> >>>
>
>
--
Andreas Aronsson
Mobil: +46 704 566 595
http://www.aron.nu
"I'd rather have friends who care than friends who agree with me."
Arlo Guthrie
More information about the jdk-dev
mailing list