HashSET.java


Below is the syntax highlighted version of HashSET.java from §3.4 Hash Tables.


/******************************************************************************
 *  Compilation:  javac HashSET.java
 *  Execution:    java HashSET
 *  Dependencies: none
 *
 *  Set implementation using Java's HashSet library.
 *  Does not allow duplicates.
 *
 ******************************************************************************/

import java.util.HashSet;
import java.util.Iterator;


/**
 *  This class represents an unordered set.
 *  It supports the usual <em>add</em>, <em>contains</em>, and <em>remove</em>
 *  methods.
 *  <p>
 *  This implementation uses a hash table.
 *  The <em>add</em>, <em>contains</em>, and <em>remove</em>
 *  methods take constant time on average under the uniform hashing assumption.
 *  <p>
 *  For additional documentation,
 *  see <a href="https://algs4.cs.princeton.edu/44hash">Section 4.4</a> of
 *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
 */

public class HashSET<Key> implements Iterable<Key> {
    private HashSet<Key> set;

    /**
     * Create an empty set.
     */
    public HashSET() {
        set = new HashSet<Key>();
    }

    /**
     * Is this set empty?
     */
    public boolean isEmpty() {
        return set.isEmpty();
    }

    /**
     * Add the key to this set.
     */
    public void add(Key key) {
        set.add(key);
    }

    /**
     * Does this set contain the given key?
     */
    public boolean contains(Key key) {
        return set.contains(key);
    }

    /**
     * Remove the given key from this set.
     */
    public void remove(Key key) {
        set.remove(key);
    }

    /**
     * Return the number of keys in this set.
     */
    public int size() {
        return set.size();
    }

    /**
     * Return an Iterator for this set.
     */
    public Iterator<Key> iterator() {
        return set.iterator();
    }

    /**
     * Return the union of this set with that set.
     */
    public HashSET<Key> union(HashSET<Key> that) {
        HashSET<Key> c = new HashSET<Key>();
        for (Key x : this)
            c.add(x);
        for (Key x : that)
            c.add(x);
        return c;
    }

    /**
     * Return the intersection of this set with that set.
     */
    public HashSET<Key> intersects(HashSET<Key> that) {
        HashSET<Key> c = new HashSET<Key>();
        if (this.size() < that.size()) {
            for (Key x : this) {
                if (that.contains(x)) c.add(x);
            }
        }
        else {
            for (Key x : that) {
                if (this.contains(x)) c.add(x);
            }
        }
        return c;
    }

   /***************************************************************************
    * Test routine.
    ***************************************************************************/
    public static void main(String[] args) {
        HashSET<String> set = new HashSET<String>();


        // insert some keys
        set.add("www.cs.princeton.edu");
        set.add("www.cs.princeton.edu");    // overwrite old value
        set.add("www.princeton.edu");
        set.add("www.math.princeton.edu");
        set.add("www.yale.edu");
        set.add("www.amazon.com");
        set.add("www.simpsons.com");
        set.add("www.stanford.edu");
        set.add("www.google.com");
        set.add("www.ibm.com");
        set.add("www.apple.com");
        set.add("www.slashdot.com");
        set.add("www.whitehouse.gov");
        set.add("www.espn.com");
        set.add("www.snopes.com");
        set.add("www.movies.com");
        set.add("www.cnn.com");
        set.add("www.iitb.ac.in");


        StdOut.println(set.contains("www.cs.princeton.edu"));
        StdOut.println(!set.contains("www.harvardsucks.com"));
        StdOut.println(set.contains("www.simpsons.com"));
        StdOut.println();


        // print out all keys in the set
        for (String s : set) {
            StdOut.println(s);
        }

    }

}


Copyright © 2000–2019, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 09:16:19 EDT 2022.