Collectors.toSet() small performance improvement

Aleksey Shipilev aleksey.shipilev at gmail.com
Tue Aug 9 07:25:56 UTC 2016


On 08/09/2016 10:19 AM, Tagir F. Valeev wrote:
> AS> Good trick, but does it work properly with the sets that care about the
> AS> add order, e.g. LinkedHashSet? I guess our saving grace here is
> AS> Collector.toSet() is declared UNORDERED, so we can disturb the encounter
> AS> order without violating the API contract.
> 
> This implementation is bound to HashSet which is created right here,
> in supplier:
> 
> return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add,... )
> 
> No other set implementation is possible here. Users who want to use
> custom set should use Collectors.toCollection(LinkedHashSet::new)
> which I'm not going to change as indeed here order might be important.

Right. (seeps some green tea trying to wake up)

Looks good then.

Thanks,
-Aleksey



More information about the core-libs-dev mailing list