// Very bare bones implementation of HashSet to introduce the idea of hashing. // Does not handle collisions. public class HashStringSet { private String[] hashTable; private int size; public static final int DEFAULT_CAPACITY = 10; // Constructs a new, empty set public HashStringSet() { hashTable = new String[DEFAULT_CAPACITY]; size = 0; } // Adds the specified element to this set if it is not already present. public void add(String value) { if (!contains(value)) { int index = hashIndex(value); hashTable[index] = value; size++; } } // Returns true if this set contains the specified value and false otherwise public boolean contains(String value) { int index = hashIndex(value); return value.equals(hashTable[index]); } // Returns the index that this element belongs at private int hashIndex(String value) { int index = Math.abs(value.hashCode() % hashTable.length); return index; } // Returns the number of elements in this set public int size() { return this.size; } }