- Type Parameters:
T
- the type contained
- All Implemented Interfaces:
- Iterable<T>, Collection<T>, Set<T>
- Enclosing class:
- CollectionFactory
public static class CollectionFactory.ConditionalCopySet<T>
extends Object
implements Set<T>
a set implementation that uses a delegate collection for all read-only operations and makes a
copy if changes are attempted. Useful for cheap defensive copies: no costly rehashing on the
original collection is made unless changes are attempted. Changes are not mirrored back to
the original collection, although changes to the original set BEFORE changes to the copy are
reflected in the copy. If the source collection is not supposed to change, then this
collection behaves just like a regular defensive copy; if the source collection can change,
then this collection should be built from a cheap copy of the original collection. For
example, if the source collection is a set, it can be copied into a list; the cost of the
copy operation from set to list is approximately 1/3 of the cost of copying into a new
HashSet. This is not efficient if the most common operations performed on the copy are
contains() or containsAll(), since they are more expensive for lists wrt sets; a counter for
these calls is maintained by the collection, so if a large number of contains/containsAll
calls takes place, the delegate is turned into a regular set. This implementation is not
threadsafe even if the source set is: there is no lock during the copy, and the new set is
not threadsafe.