common
Class WeakHashSet

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet
          extended by common.WeakHashSet
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection, java.util.Set

public class WeakHashSet
extends java.util.AbstractSet

A HashSet class that uses weak references to its elements. This means that if the only reference to an object is from this set, the object will be garbage-collected and removed from this set. The current implementation is built on WeakHashMap: it's just a map where every key is mapped to Boolean.TRUE. It would be a bit more elegant (and save some memory) to write this class from scratch. But it can't be built on top of an ordinary HashSet, because we want to store WeakReference objects while using the equals and hashCode methods of their referents.


Constructor Summary
WeakHashSet()
          Creates a new, empty WeakHashSet.
WeakHashSet(java.util.Collection c)
          Creates a new WeakHashSet whose elements are weak references to the elements of the given collection.
 
Method Summary
 boolean add(java.lang.Object o)
           
 boolean contains(java.lang.Object o)
           
 java.util.Iterator iterator()
           
 boolean remove(java.lang.Object o)
           
 int size()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray
 

Constructor Detail

WeakHashSet

public WeakHashSet()
Creates a new, empty WeakHashSet.


WeakHashSet

public WeakHashSet(java.util.Collection c)
Creates a new WeakHashSet whose elements are weak references to the elements of the given collection.

Method Detail

size

public int size()
Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.Set
Specified by:
size in class java.util.AbstractCollection

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.Set
Overrides:
contains in class java.util.AbstractCollection

iterator

public java.util.Iterator iterator()
Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.Set
Specified by:
iterator in class java.util.AbstractCollection

add

public boolean add(java.lang.Object o)
Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.Set
Overrides:
add in class java.util.AbstractCollection

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.Set
Overrides:
remove in class java.util.AbstractCollection