Weak HashSet from objectweb jac : Weak Set « Collections Data Structure « Java






Weak HashSet from objectweb jac

  
// Revised from objectweb jac

import java.lang.Cloneable;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.WeakHashMap;


public class WeakHashSet extends AbstractSet
    implements Set
{
    private transient WeakHashMap map;

    public WeakHashSet() {
        map = new WeakHashMap();
    }
    public WeakHashSet(Collection c) {
        map = new WeakHashMap(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
    }
    public WeakHashSet(int initialCapacity, float loadFactor) {
        map = new WeakHashMap(initialCapacity, loadFactor);
    }
    public WeakHashSet(int initialCapacity) {
        map = new WeakHashMap(initialCapacity);
    }

    public Iterator iterator() {
        return map.keySet().iterator();
    }

    public int size() {
        return map.size();
    }
    public boolean isEmpty() {
        return map.isEmpty();
    }

    public boolean contains(Object o) {
        return map.containsKey(o);
    }

    public boolean add(Object o) {
        return map.put(o, Boolean.TRUE)==null;
    }
    public boolean remove(Object o) {
        return map.remove(o)==Boolean.TRUE;
    }
    public void clear() {
        map.clear();
    }
}

   
    
  








Related examples in the same category

1.Weak HashSet
2.Weak HashSet from TJDO
3.Set which holds its members by using of WeakReferences.
4.A weak HashSet: element stored in the WeakHashSet might be garbage collected