// This implementation does not work for // collisions, but it introduces the idea // of using hash codes 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; } 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) { int index = hashIndex(element); return hashTable[index] != 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; } int index = hashIndex(element); hashTable[index] = 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; } int index = hashIndex(element); hashTable[index] = null; size--; return true; } // Returns the number of elements in this set public int size() { return this.size; } // Returns true if this set contains no elements public boolean isEmpty() { return this.size == 0; } }