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

Peter Levart peter.levart at gmail.com
Thu Nov 2 12:31:05 UTC 2017


On 11/02/2017 12:51 AM, Stuart Marks wrote:
> On 11/1/17 10:45 AM, Tagir Valeev wrote:
>> Set.of:
>> +        if (coll instanceof 
>> ImmutableCollections.AbstractImmutableSet) {
>> +            return (Set<E>)coll;
>> +        } else {
>> +            return (Set<E>)Set.of(coll.stream().distinct().toArray());
>> I think that good old Set.of(new HashSet<>(coll).toArray()) would
>> produce less garbage. distinct() also maintains HashSet internally,
>> but it removes the SIZED characteristic, so instead of preallocated
>> array you will have a SpinedBuffer which is less efficient than
>> AbstractCollection.toArray() implementation which just allocates the
>> array of exact size. What do you think?
> Oh yes, good point. I had initially used stream().distinct() because I 
> wanted to use distinct()'s semantics of preserving the first equal 
> element among duplicates. But since I removed that requirement from 
> the spec, using a HashSet as you suggest is much simpler. 

Why not using:


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 

Regards, Peter

More information about the core-libs-dev mailing list