Implements a hash set that is concurrent, backed by a concurrent hash map, and memory efficient.
The memory efficiency comes from the fact that the map is initialized only when the set contains
more than one element. When it contains a single element it is simply stored in a field. When the
set is empty the field is null. In situations where is likely that the set will contain no or
only one element there is no memory overhead incurred by allocating the map.
The value is not referenced by objects of this class but all operations use a
AtomicReferenceFieldUpdater
to a storage location of type
Object
. This location
is then populated with three possible values:
- No elements: the location is set to
null
- One element: the single element is stored directly in this location.
- Multiple elements: the location points to a
ConcurrentHashMap
with the elements as
the keys. The values of the map are unused.