classMyHashSet{ privatestaticfinalint BASE = 769; private LinkedList[] data;
/** Initialize your data structure here. */ publicMyHashSet(){ data = new LinkedList[BASE]; for (int i = 0; i < BASE; ++i) { data[i] = new LinkedList<Integer>(); } } publicvoidadd(int key){ int h = hash(key); Iterator<Integer> iterator = data[h].iterator(); while (iterator.hasNext()) { Integer element = iterator.next(); if (element == key) { return; } } data[h].offerLast(key); } publicvoidremove(int key){ int h = hash(key); Iterator<Integer> iterator = data[h].iterator(); while (iterator.hasNext()) { Integer element = iterator.next(); if (element == key) { data[h].remove(element); return; } } } /** Returns true if this set contains the specified element */ publicbooleancontains(int key){ int h = hash(key); Iterator<Integer> iterator = data[h].iterator(); while (iterator.hasNext()) { Integer element = iterator.next(); if (element == key) { returntrue; } } returnfalse; }