Skip to content

Instantly share code, notes, and snippets.

@imduffy15
Created January 10, 2013 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imduffy15/4504610 to your computer and use it in GitHub Desktop.
Save imduffy15/4504610 to your computer and use it in GitHub Desktop.
class HashSet<T> {
private LinkedSet<T>[] hashTable;
@SuppressWarnings("unchecked")
HashSet() {
hashTable = (LinkedSet<T>[])(new LinkedSet[1000]);
for(int i=0;i<hashTable.length;i++) {
hashTable[i] = new LinkedSet<T>();
}
}
private int hash(T t) {
return Math.abs(t.hashCode()%hashTable.length);
}
int size() {
int numItems = 0;
for(LinkedSet<T> miniSet : hashTable) {
numItems = numItems+miniSet.size();
}
return numItems;
}
boolean contains(T t) {
return hashTable[hash(t)].contains(t);
}
boolean add(T t) {
return hashTable[hash(t)].add(t);
}
boolean remove(T t) {
return hashTable[hash(t)].remove(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment