E
- the type of elements maintained by this set.public class LinkedWeakIdentityHashSet<E> extends AbstractSet<E>
Hash set that holds its elements through weak references and has a predictable iteration order.
Internally, this set wraps its elements in WeakReference
s and stores
them inside a HashMap
. Unlike a WeakHashMap
, this set uses
object identity and not equality when comparing two objects. All elements
also form a linked list, so the iterator returns the elements in the order in
which they were added. This set does not support null
elements.
Trying to add a null
element causes a
NullPointerException
.
Elements in this set are hold through WeakReference
s, so the fact
that an object has been added to this set will not keep it from being garbage
collected. As the time of garbage collection is unpredictable, this set might
change unexpectedly. For example:
size()
may return two different numbers
where the second number is smaller than the first one, without explicitly
removing any elements from the set.AbstractCollection.isEmpty()
might return false
and then
true
even though the set has not been modified in between.However, because this set compares elements based on object identity, not equality, some operations are guaranteed to be consistent. For example:
contains(Object)
will always return
the same result.contains(Object)
will always
return true
until the object is removed explicitly.
Iterators returned by the iterator()
method have some weak
consistency guarantees:
hasNext()
returned true
, the
next()
method will always return a non-null element
and will never throw a NoSuchElementException
.ConcurrentModificationException
.
However, the iterator will throw such an exception if (after creating the
iterator) this set is modified explicitly through other means than the
iterator's remove()
method.
The performance of this set should be roughly equivalent to the performance
of a LinkedHashSet
. However, occasionally stale elements (elements
that have been found by the garbage collector to be only weakly reachable)
are removed, so an operation which theoretically is O(1) is actually O(n)
where n is the number of entries that have been selected by the garbage
collector since the last removal of stale elements. Stale elements are
removed each time one of this set's methods is called.
This set is not safe for use by multiple threads. In this case, access to
this set (and its iterators) needs to be synchronized, e.g. by wrapping it
with Collections.synchronizedSet(java.util.Set)
. Unlike most
collection implementations, this set is not even safe for read-only use by
multiple threads because a read operation can trigger the removal of stale
elements.
Constructor and Description |
---|
LinkedWeakIdentityHashSet()
Creates an empty set with the default initial capacity and load factor of
the underlying
HashMap (typically 16 and 0.75). |
LinkedWeakIdentityHashSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
|
LinkedWeakIdentityHashSet(int initialCapacity)
Creates an empty set with the specified initial capacity and the default
load factor of the underlying
HashMap (typically 0.75). |
LinkedWeakIdentityHashSet(int initialCapacity,
float loadFactor)
Creates an empty set with the specified initial capacity and load factor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e) |
void |
clear() |
boolean |
contains(Object o) |
Iterator<E> |
iterator() |
boolean |
remove(Object o) |
int |
size() |
equals, hashCode, removeAll
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
public LinkedWeakIdentityHashSet()
HashMap
(typically 16 and 0.75).public LinkedWeakIdentityHashSet(int initialCapacity)
HashMap
(typically 0.75).initialCapacity
- the initial capacity of the hash table.IllegalArgumentException
- if the initial capacity is less than zero.public LinkedWeakIdentityHashSet(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity of the hash table.loadFactor
- the load factor of the hash table.IllegalArgumentException
- if the initial capacity is less than zero, or if the load
factor is not positive.public LinkedWeakIdentityHashSet(Collection<? extends E> c)
HashMap
has a load factor of 0.75 and an initial
capacity sufficient to hold all elements from the collection.c
- the collection whose elements are to be placed into this set.NullPointerException
- if the specified collection is null
or contains
null
elements.public boolean add(E e)
add
in interface Collection<E>
add
in interface Set<E>
add
in class AbstractCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in interface Set<E>
clear
in class AbstractCollection<E>
public boolean contains(Object o)
contains
in interface Collection<E>
contains
in interface Set<E>
contains
in class AbstractCollection<E>
public boolean remove(Object o)
remove
in interface Collection<E>
remove
in interface Set<E>
remove
in class AbstractCollection<E>
public int size()
size
in interface Collection<E>
size
in interface Set<E>
size
in class AbstractCollection<E>
Copyright © 2014–2016 aquenos GmbH. All rights reserved.