src/main/java/org/jboss/util/collection/WeakSet.java
public Iterator iterator() {
      return new Iterator() {

            /** The set's iterator */
            Iterator iter = set.iterator();

            /** JBCOMMON-24, handle null values and multiple invocations of hasNext() */
            Object UNKNOWN = new Object();
            
            /** The next available object. */
            Object next = UNKNOWN;

            public boolean hasNext() {
               if (next != UNKNOWN) {
                  return true;
               }
               while (iter.hasNext()) {
                  WeakObject weak = (WeakObject)iter.next();
                  Object obj = null;
                  if (weak != null && (obj = weak.get()) == null) {
                     // object has been reclaimed by the GC
                     continue;
                  }
                  next = obj;
                  return true;
               }
               return false;
            }

            public Object next() {
               if ((next == UNKNOWN) && !hasNext()) {
                  throw new NoSuchElementException();
               }
               Object obj = next;
               next = UNKNOWN;

               return obj;
            }

            public void remove() {
               iter.remove();
            }
         };
   }
public Iterator iterator() {
      return new Iterator() {

            /** The set's iterator */
            Iterator iter = set.iterator();

            /** JBCOMMON-24, handle null values and multiple invocations of hasNext() */
            Object UNKNOWN = new Object();
            
            /** The next available object. */
            Object next = UNKNOWN;

            public boolean hasNext() {
               if (next != UNKNOWN) {
                  return true;
               }
               while (iter.hasNext()) {
                  WeakObject weak = (WeakObject)iter.next();
                  Object obj = null;
                  if (weak != null && (obj = weak.get()) == null) {
                     // object has been reclaimed by the GC
                     continue;
                  }
                  next = obj;
                  return true;
               }
               return false;
            }

            public Object next() {
               if ((next == UNKNOWN) && !hasNext()) {
                  throw new NoSuchElementException();
               }
               Object obj = next;
               next = UNKNOWN;

               return obj;
            }

            public void remove() {
               iter.remove();
            }
         };
   }
Return an iteration over the elements in the set.

@return  An iteration over the elements in the set.