|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectAbstractCollection<E>
AbstractSet<E>
WeakHashSet<E>
E - The type of elements in the set.@ThreadSafe public class WeakHashSet<E>
A set of objects hold by weak references. An entry in a 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.
Optimizing memory use in factory implementations
The WeakHashSet class has a get(T) 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(T) 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 = WeakHashSet.newInstance(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.
WeakHashMap
| utility/geotk-utility (download) | View source code for this class |
| Constructor Summary | |
|---|---|
protected |
WeakHashSet(Class<E> type)
Constructs a WeakHashSet for elements of the specified type. |
| Method Summary | ||
|---|---|---|
boolean |
add(E obj)
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 obj)
Returns true if this set contains the specified element. |
|
|
get(T object)
Returns an object equals to the specified object, if present. |
|
Class<E> |
getElementType()
Returns the element type. |
|
Iterator<E> |
iterator()
Returns an iterator over the elements contained in this collection. |
|
static
|
newInstance(Class<E> type)
Constructs a WeakHashSet for elements of the specified type. |
|
boolean |
remove(Object obj)
Removes a single instance of the specified element from this set, if it is present |
|
int |
size()
Returns the count of element in this set. |
|
E[] |
toArray()
Returns a view of this set as an array. |
|
|
unique(T object)
Returns an object equals to object if such an object already exist in this
WeakHashSet. |
|
void |
uniques(E[] objects)
Iteratively call unique(Object) for an array of objects. |
|
| Methods inherited from class AbstractSet |
|---|
equals, hashCode, removeAll |
| Methods inherited from class AbstractCollection |
|---|
addAll, containsAll, isEmpty, retainAll, toArray, toString |
| Methods inherited from class Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface Collection |
|---|
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray |
| Methods inherited from interface Set |
|---|
addAll, containsAll, isEmpty, retainAll, toArray |
| Constructor Detail |
|---|
protected WeakHashSet(Class<E> type)
WeakHashSet for elements of the specified type.
type - The type of the element to be included in this set.| Method Detail |
|---|
public static <E> WeakHashSet<E> newInstance(Class<E> type)
WeakHashSet for elements of the specified type.
E - The type of elements in the set.type - The type of elements in the set.
public Class<E> getElementType()
getElementType in interface CheckedCollection<E>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 obj)
false.
add in interface Collection<E>add in interface Set<E>add in class AbstractCollection<E>obj - Element to be added to this set.
true if this set did not already contain the specified element.public boolean remove(Object obj)
remove in interface Collection<E>remove in interface Set<E>remove in class AbstractCollection<E>obj - element to be removed from this set, if present.
true if the set contained the specified element.public <T extends E> T get(T object)
object,
then this method returns null.
T - The type of the element to get.object - The element to get.
null otherwise.unique(Object)public boolean contains(Object obj)
true if this set contains the specified element.
contains in interface Collection<E>contains in interface Set<E>contains in class AbstractCollection<E>obj - Object to be checked for containment in this set.
true if this set contains the specified element.public <T extends E> T unique(T object)
object if such an object already exist in this
WeakHashSet. Otherwise, adds object to this WeakHashSet.
This method is equivalents to the following code:
if (object != null) {
Object current = get(object);
if (current != null) {
return current;
} else {
add(object);
}
}
return object;
T - The type of the element to get.object - The element to get or to add in the set if not already presents.
object otherwise.public void uniques(E[] objects)
unique(Object) for an array of objects.
This method is equivalents to the following code:
for (int i=0; i<objects.length; i++) {
objects[i] = unique(objects[i]);
}
objects - On input, the objects to add to this set if not already present. On output,
elements that are equal, but where every
reference to an instance already presents in this set has been replaced by
a reference to the existing instance.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>public Iterator<E> iterator()
iterator in interface Iterable<E>iterator in interface Collection<E>iterator in interface Set<E>iterator in class AbstractCollection<E>
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||