Design for collections upgrades

Alessio Stalla alessiostalla at
Thu Mar 10 11:53:52 PST 2011

On Thu, Mar 10, 2011 at 7:56 PM, Pavel Minaev <int19h at> wrote:
> So if I call filter on a TreeSet reference, then I get a TreeSet; but if I
> upcast it to Set and call filter, I get HashSet?

No, if I correctly understand how defender methods are supposed to
work. Set would have a defender method - say, newInstance() -
defaulting to the creation of a HashSet. But TreeSet could actually
implement newInstance() to return a TreeSet, perhaps optimized from
the current state of the receiver TreeSet instance (e.g. by
preallocating enough space to contain the same number of elements as
the receiver). This isn't automatically a general mechanism (Set
implementations need to implement newInstance() in order to return
something different from HashSet), but imho it's a valid point of
extension, and all the built-in Set implementations could honor its
meaning and document that (the same way as, e.g., all built-in
Collection implementations have an empty constructor and a "copy


More information about the lambda-dev mailing list