// Tyler Mi // A class representing a set of Strings // This class doesn't work because it doesn't handle collisions public class HashStringSet { private String[] hashTable; private int size; // Constructs a new, empty set with the given underlying capacity public HashStringSet(int capacity) { hashTable = new String[capacity]; size = 0; } // Returns the index that the given element would be at in the hash table private int hashIndex(String element) { int index = Math.abs(element.hashCode() % hashTable.length); return index; } // Returns true if this set contains the specified element and false otherwise public boolean contains(String element) { return hashTable[hashIndex(element)] != null; } // Adds the specified element to this set if it is not already present. // Returns true if the element was not already present and false otherwise public boolean add(String element) { if (contains(element)) { return false; } hashTable[hashIndex(element)] = element; size++; return true; } // Removes the specified element from this set if it is present. // Returns true if the element was present and false otherwise public boolean remove(String element) { if (!contains(element)) { return false; } hashTable[hashIndex(element)] = null; size--; return true; } // Returns the number of elements in this set public int size() { return size; } // Returns true if this set contains no elements public boolean isEmpty() { return size == 0; } }