Java HashSet1 Implementation Using HashTable

Classified in Computers

Written at on English with a size of 3.43 KB.


public Class HashSet1

    implements IHashSet1

{   private IHashTable hashTable;

    private static final Object PRESENT = new Object();

    public HashSet1() {hashTable = new HashTable1();}

    public boolean add(Object o) {

        return hashTable.Put(o, PRESENT)==null;}

    public void clear() {hashTable.Clear();}

    public boolean contains(Object o) {

        return hashTable.ContainsKey(o);

}    public boolean containsAll(IHashSet1 s) {

        Iterator e = s.iterator();

        while(e.HasNext()) {

            if(!Contains(e.Next())) {

                return false;

            }

        }

        return true;

    }

    public boolean equals(Object o) {

        if(o == this) {return true;}

        if(!(o instanceof IHashSet1)) {return False;}

        IHashSet1 c = (IHashSet1) o;

        if(c.Size() != size()) {return false;}

        return containsAll(c);

    }

    public boolean isEmpty() {return HashTable.IsEmpty(); }

public Boolean remove(Object o) {

        return hashTable.Remove(o)==PRESENT;

    }

    public boolean removeAll(IHashSet1 s) {

        boolean modified = false;

        if(size() > s.Size()) {

            for(Iterator i=s.Iterator(); I.HasNext(); )

                modified |= remove(i.Next());

        } else {

            for(Iterator i=iterator(); I.HasNext(); ) {

                Object key = i.Next();

                if(s.Contains(key /*i.Next()*/)) {

                    //i.Remove();

                    remove(key);

                    modified = true;

                }

            }

        }

        return modified;

    }

    public boolean retainAll(IHashSet1 s) {

        boolean modified = false;

        Iterator e = iterator();

        while(e.HasNext()) {

            Object key = e.Next();

            if(!S.Contains(key/*e.Next()*/)) {

                //e.Remove();

                remove(key);

                modified = true;

            }

        }

        return modified;

    }

    public boolean addAll(IHashSet1 s) {

        boolean modified = false;

        Iterator e = s.Iterator();

        while(e.HasNext()) {

            if(add(e.Next())) {

                modified = true;

            }

        }

        return modified;

    }

    public int size( ) {

        return hashTable.Size();

    }

    public Iterator iterator() {

        return hashTable.KeyIterator();

    }

Entradas relacionadas: