FileDocCategorySizeDatePackage
WeakHashSet.javaAPI DocExample7604Sun Dec 14 22:47:38 GMT 2003oreilly.hcj.references

WeakHashSet

public class WeakHashSet extends AbstractSet implements Set
Implements a HashSet where the objects given are stored in weak references.

Uses the WeakHashMap class as a backing store to implement a set of objects that are stored as weak references. All information concerning using keys in the WeakHashMap class pertain to this class and it is reccomended that the user of this class review that material before using the class.

Because this set contains only weak references, it is not serializable. If one tried to serialize a weak reference, the results would be highly unpredictable as the object could likely vanish from memory before the proces was even completed. Users of this class must use transient when the containing class uses this set.

Because of the semantics of the weak references, the value null is not allowed in this set.

This collection is not identity based but equality based. This can cause some confusion as you cannot put in two objects whose equals() methods return true. It also means that an object being held is not necessarily the same one that the user is holding. For example, you could have a String with the value 'fred' at memory location X and ther could be another String with the value 'fred' at memory location Y. The first instance is in the set but the second isn't.

author
Robert Simmons jr.
version
$Revision: 1.8 $
see
java.lang.util.WeakHashMap
see
java.lang.ref.WeakReference

Fields Summary
private static final Object
DUMMY
Dummy value used as a value object.
WeakHashMap
backingStore
Holds the backing store.
Constructors Summary
public WeakHashSet()
Constructs a new empty WeakHashSet with default values passed the the backing store.

see
java.util.WeakHashMap#WeakHashMap()


	                	 
	  
		backingStore = new WeakHashMap();
	
public WeakHashSet(Collection c)
Constructs a new WeakHashSet with default values passed the the backing store and fills it with the given collection. Note that duplicates in the collection will merely be overwritten

see
java.util.WeakHashMap#WeakHashMap(Collection)

		backingStore = new WeakHashMap(Math.max((int)(c.size() / .75f) + 1, 16));
		addAll(c);
	
public WeakHashSet(int initialCapacity, float loadFactor)
Constructs a new WeakHashSet with the values given passed the the backing store.

see
java.util.WeakHashMap#WeakHashMap(int, float)

		backingStore = new WeakHashMap(initialCapacity, loadFactor);
	
public WeakHashSet(int initialCapacity)
Constructs a new WeakHashSet with the values given passed the the backing store.

see
java.util.WeakHashMap#WeakHashMap(int)

		backingStore = new WeakHashMap(initialCapacity);
	
Methods Summary
public booleanadd(java.lang.Object o)
{@inheritDoc}

throws
NullPointerException If the user tries to add null to the set.

		if (o == null) {
			throw new NullPointerException();
		}

		return backingStore.put(o, DUMMY) == null;
	
public booleanaddAll(java.util.Collection c)
{@inheritDoc}

see
#add(Object)

		boolean changed = false;
		Iterator iter = c.iterator();

		while (iter.hasNext()) {
			changed = (changed | (backingStore.put(iter.next(), DUMMY) != DUMMY));
		}

		return changed;
	
public voidclear()
{@inheritDoc}

		backingStore.clear();
	
protected java.lang.Objectclone()
{@inheritDoc}

		throw new CloneNotSupportedException();
	
public booleancontains(java.lang.Object o)
{@inheritDoc}

		return backingStore.containsKey(o);
	
public booleancontainsAll(java.util.Collection c)
{@inheritDoc}

		return backingStore.keySet()
		                   .containsAll(c);
	
public booleanequals(java.lang.Object o)
{@inheritDoc}

		return backingStore.equals(o);
	
public inthashCode()
Returns the hash code value for this set.

Gives back the hashCode for the backing store key set. The user should be aware, however, that this hash code can change without user intervention as the objects in the collection can easily be collected microseconds after completetion of the method. It is not reccomended that the user rely on this hash code for consistency

return
The hashcode for this object.

		return backingStore.keySet()
		                   .hashCode();
	
public booleanisEmpty()
{@inheritDoc}

		return backingStore.keySet()
		                   .isEmpty();
	
public java.util.Iteratoriterator()
Returns an iterator over the elements contained in this collection.

Note that this iterator is extremely volatile because the user may iterate over an element in the set and find seconds later that it has been removed. This is because of the semantics of weak references which act like a second thread is silently modifying the collection. For this reason, it is advisable that if the user wants to do something with the set that they maintain a strong reference to the object and not rely on it being in the collection for them.

This iterator is fail fast and WeakReference transparrent. By this we mean that the iterator simply ignores objects pending in the reference queue for cleanup.

return
The iterator.

		return backingStore.keySet()
		                   .iterator();
	
public booleanremove(java.lang.Object o)
{@inheritDoc}

		return backingStore.keySet()
		                   .remove(o);
	
public booleanremoveAll(java.util.Collection c)
{@inheritDoc}

		return backingStore.keySet()
		                   .removeAll(c);
	
public booleanretainAll(java.util.Collection c)
{@inheritDoc}

		return backingStore.keySet()
		                   .retainAll(c);
	
public intsize()
{@inheritDoc}

		return backingStore.keySet()
		                   .size();
	
public java.lang.Object[]toArray()
{@inheritDoc}

		return backingStore.keySet()
		                   .toArray();
	
public java.lang.Object[]toArray(java.lang.Object[] a)
{@inheritDoc}

		return backingStore.keySet()
		                   .toArray(a);
	
public java.lang.StringtoString()
{@inheritDoc}

		return backingStore.keySet()
		                   .toString();