/* * Kyle Pierce * March 11, 2019 * * This is a quick and dirty HashSet implementation */ public class HashSet implements Set { public int size; public HashNode[] table; /* * Constructs a new HashSet using the given size */ public HashSet(int size) { table = (HashNode[]) new HashNode[size]; } /* * Adds the given element to this HashSet * Returns true if the addition was successful */ public boolean add(E e) { if (contains(e)) { return false; } int i = hash(e); table[i] = new HashNode(e, table[i]); return true; } /* * Returns true if the given element is contained in this HashSet * and false otherwise */ public boolean contains(E e) { int i = hash(e); HashNode current = table[i]; while (current != null) { if (current.data.equals(e)) { return true; } current = current.next; } return false; } /* * Removes the given element from this HashSet * Returns true if the removal was successful */ public boolean remove(E e) { if (!contains(e)) { return false; } int i = hash(e); if (table[i].data.equals(e)) { table[i] = table[i].next; } else { HashNode current = table[i]; while (!current.next.data.equals(e)) { current = current.next; } current.next = current.next.next; } return true; } /* * Returns the size of this HashSet */ public int size() { return size; } /* * Returns true if this HashSet is empty, false otherwise */ public boolean isEmpty() { return size == 0; } /* * Returns the hash of the given element */ private int hash(E e) { return Math.abs(e.hashCode()) % table.length; } /* * Represents a single node in the chain in the hash table */ private static class HashNode { public E data; public HashNode next; /* * Constructs a new node with the given value and next link */ public HashNode(E data, HashNode next) { this.data = data; this.next = next; } } }