RFR(m): 8177290 add copy factory methods for unmodifiable List, Set, Map

Patrick Reinhart patrick at reini.net
Fri Nov 10 08:49:47 UTC 2017


Hi Stuart,

After having thought over your arguments about copyOf versus unmodifiableCopyOf length discussion (also with my colleague) I was thinking about why not create additional X.of(X) methods instead of X.copyOf(X). It would seem to me a logical enhancement in the sense of the existing API though.

-Patrick

> Am 02.11.2017 um 23:04 schrieb Stuart Marks <stuart.marks at oracle.com>:
> 
> 
>> Why not using:
>> 
>>     coll.stream().collect(Collectors.toImmutableSet())
>> 
>> As Collectors.toImmutableSet() is currently implemented, with serial Stream it will create a single HashSet, add all the elements to it and call Set.of(HashSet.toArray()) with it. Pretty much the same as what Tagir proposes, but the Collector could be made more efficient in the future and with it, the optimization would automatically extend to Set.copyOf()...
> This is mainly about whether Set.copyOf() is implemented in terms of Collectors.toUnmodifiableSet(), or vice-versa, which then calls Set.of(T[]) to do the actual creation. Some future optimization will probably replace both of these implementations with calls to JDK internal methods that can bypass the extra copying, so it doesn't really matter which one of these calls the other right now.
> 
> s'marks
> 



More information about the core-libs-dev mailing list