RFR: 8180352: Add Stream.toList() method

Alan Snyder javalists at cbfiddle.com
Fri Nov 6 18:00:22 UTC 2020


>> This discussion of unmodifiable lists brings me back to the thought that
>> there would be good client-side reasons for inserting an UnmodifiableList
>> interface as a parent of LIst

> On Nov 6, 2020, at 1:14 AM, Remi Forax <forax at univ-mlv.fr <mailto:forax at univ-mlv.fr>> wrote:
> 
> 
> This question is asked at least every six months since 1998
> https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html#a1 <https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html#a1>

The question that Simon asked is not exactly the question that is answered in this link.

The question of whether List should be a subtype of (some kind of) ImmutableList is answered in
Stuart’s stack overflow answer (https://stackoverflow.com/a/57926310/1441122 <https://stackoverflow.com/a/57926310/1441122>). The answer
is that it should not.

The question answered in the link is basically a straw man: why not capture every conceivable
semantic distinction in the collections type system. And the answer is, not surprisingly, that
there would be way too many types.

But a question that deserves ongoing review is whether Java should support immutable collections
using a separate type hierarchy. In other words, Immutable List would not be a subtype of List
and List would not be a subtype of Immutable List. The linked answer says:
"Adding this support to the type hierarchy requires four more interfaces.” Four additional
interfaces sounds like a small cost for a significant benefit. As Java evolves to better support
functional programming styles, immutable collections seems like an obvious next step.

Much could be written, and probably has been, about the disadvantages of basing the
collections framework on mutable collections. Let me just remark that in addition to the
already mentioned UnsupportedOperationException, there is also the more subtle
ConcurrentModificationException, both of which would be absent in fully immutable collections.

The lack of subtyping between List and ImmutableList is not terrible. Arrays coexist with
Lists with no subtyping relationship. java.io.File coexists with java.nio.filePath with no
subtyping relationship. It means that explicit conversions are required between List and
ImmutableList. Extra copying may be needed, but there are tricks for reducing copying, and the
need for defensive copying is removed.

  Alan



More information about the core-libs-dev mailing list