E
- the type of elements in the set.public class WeakHashSet<E> extends AbstractSet<E> implements CheckedContainer<E>
WeakHashSet
will automatically
be removed when it is no longer in ordinary use. More precisely, the presence of an entry will
not prevent the entry from being discarded by the garbage collector, that is, made finalizable,
finalized, and then reclaimed. When an entry has been discarded it is effectively removed from
the set, so this class behaves somewhat differently than other Set
implementations.
If the elements stored in this set are arrays like int[]
, float[]
or
Object[]
, then the hash code computations and the comparisons are performed using
the static hashCode(a)
and equals(a1, a2)
methods defined in the Arrays
class.
WeakHashSet
class has a get(Object)
method that is not part of the
Set
interface. This get
method retrieves an entry from this set
that is equals to the supplied object. The unique(Object)
method combines a
get
followed by a add
operation if the specified object was not in the set.
This is similar in spirit to the String.intern()
method. The following example shows
a convenient way to use WeakHashSet
as an internal pool of immutable objects:
private final WeakHashSet<Foo> pool = new WeakHashSet<Foo>(Foo.class); public Foo create(String definition) { Foo created = new Foo(definition); return pool.unique(created); }Thus,
WeakHashSet
can be used inside a factory to prevent creating duplicate immutable objects.
WeakHashSet
instance can be safely used by many threads without synchronization on the part of
the caller. But if a sequence of two or more method calls need to appear atomic from other threads perspective,
then the caller can synchronize on this
.WeakHashMap
Defined in the sis-utility
module
Constructor and Description |
---|
WeakHashSet(Class<E> type)
Creates a
WeakHashSet for elements of the specified type. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Adds the specified element to this set if it is not already present.
|
void |
clear()
Removes all of the elements from this set.
|
boolean |
contains(Object element)
Returns
true if this set contains the specified element. |
E |
get(Object element)
Returns an object equals to the specified object, if present.
|
Class<E> |
getElementType()
Returns the type of elements in this set.
|
Iterator<E> |
iterator()
Returns an iterator over the elements contained in this collection.
|
boolean |
remove(Object element)
Removes a single instance of the specified element from this set, if it is present
Null values are considered never present.
|
int |
size()
Returns the count of element in this set.
|
E[] |
toArray()
Returns a view of this set as an array.
|
<T extends E> |
unique(T element)
Returns an object equals to
element if such an object already exist in this
WeakHashSet . |
equals, hashCode, removeAll
addAll, containsAll, isEmpty, retainAll, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, containsAll, isEmpty, retainAll, spliterator, toArray
parallelStream, removeIf, stream
public Class<E> getElementType()
getElementType
in interface CheckedContainer<E>
public int size()
size
in interface Collection<E>
size
in interface Set<E>
size
in class AbstractCollection<E>
public boolean add(E element) throws NullArgumentException
false
.add
in interface Collection<E>
add
in interface Set<E>
add
in class AbstractCollection<E>
element
- element to be added to this set.true
if this set did not already contain the specified element.NullArgumentException
- if the given object is null
.public boolean remove(Object element)
remove
in interface Collection<E>
remove
in interface Set<E>
remove
in class AbstractCollection<E>
element
- element to be removed from this set, if present. Can be null
.true
if the set contained the specified element.public E get(Object element)
element
, then this method returns null
.
Null values are considered never present.element
- the element to get.null
otherwise.unique(Object)
public boolean contains(Object element)
true
if this set contains the specified element.
Null values are considered never present.contains
in interface Collection<E>
contains
in interface Set<E>
contains
in class AbstractCollection<E>
element
- object to be checked for containment in this set. Can be null
.true
if this set contains the specified element.public <T extends E> T unique(T element)
element
if such an object already exist in this
WeakHashSet
. Otherwise, adds element
to this WeakHashSet
.
This method is functionally equivalents to the following code:
if (element != null) { T current = get(element); if (current != null) { return current; } else { add(element); } } return element;
T
- the type of the element to get. Can be null
.element
- the element to get or to add in the set if not already presents,
or null
if the given element was null.object
otherwise.public void clear()
clear
in interface Collection<E>
clear
in interface Set<E>
clear
in class AbstractCollection<E>
public E[] toArray()
toArray
in interface Collection<E>
toArray
in interface Set<E>
toArray
in class AbstractCollection<E>
Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.