package class9_1_Hashtable_021; import java.util.*; /** * Note: This class has problems, as discussed in class. * * @param element/key contained. */ public class ChainingHashSet implements Set { private int MAX_SIZE = 3; // 1024 == 2^10 ~= 10^3 KB KiB = 1024 bytes private List[] table; private int size; public ChainingHashSet() { table = (List[]) new List[MAX_SIZE]; size = 0; } @Override public boolean contains(Object o) { // Thanks to those who submitted the code below! (I just missed it, but it was sent before class) // Version 1: Works, see suggestions int hash = o.hashCode(); hash = Math.abs(hash) % MAX_SIZE; if(table[hash] == null) { return false; }else{ // TODO: Can use E e instead of Object e for more compiler type-checks. (Better than run-time type checks!) // TODO: Don't need loop, just use table[hash].contains for(Object e : table[hash]){ if(e.equals(o)) return true; } return false; } // Version 2: Compile errors, see TODO comments ////if key is empty, do nothing // // TODO: Convert object to key. (Can't check type easily at runtime, but must cast) // if(key == null){ // int hash = 0; // } ////else find it's hash code // else{ // int hash = hash(key.hashCode()); // TODO: Don't need hash(...). key.hashCode is enough. // // TODO: Handle negative hashes correctly. // } ////go down the set and see if any object has that hash code // // TODO: Use hash to find spot in table. (It's an index now.) // // TODO: Don't need since Entry is a non-static nested class. // // TODO: It appears your entry implements linked list. We just wrapped it for the example code in class. // // But your implementation looks about right. // // TODO: Return true/false for the contains method. // for (Entry e = table[indexFor(hash, table.length)]; e!= null; e = e.next;){ // object k; ////if something equals the hash code // if(e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))){ ////return the object // return e; // } ////else return null // return null; // // Version 3 // boolean contains = false; // int hash = o.hashCode(); // hash = Math.abs(hash)%MAX_SIZE; // // if (table[hash] != null && table[hash].equals(o)) // TODO: a LinkedList of chained elements cannot be equal // // to an object of type E // { // contains = true; // } // return contains; } @Override public boolean add(E key) { if(key == null) { throw new NullPointerException("Key is null. (Null keys not accepted in this implementation.)"); } boolean isChanged = false; int hash = key.hashCode(); hash = Math.abs(hash) % MAX_SIZE; if(table[hash] == null) { table[hash] = new LinkedList(); table[hash].add(key); isChanged = true; size++; } else { if(table[hash].contains(key)) { System.out.println("DEBUG: Key found; no change. Key:"+key); isChanged = false; } else { System.out.println("DEBUG: Key not found. Adding: "+ key); table[hash].add(key); isChanged = true; size++; } } return isChanged; } @Override public void clear() { for(int i=0; i iterator() { throw new UnsupportedOperationException("Not supported"); } @Override public Object[] toArray() { throw new UnsupportedOperationException("Not supported"); } @Override public T[] toArray(T[] a) { throw new UnsupportedOperationException("Not supported"); } @Override public boolean containsAll(Collection c) { throw new UnsupportedOperationException("Not supported"); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException("Not supported"); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException("Not supported"); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException("Not supported"); } }