Created
January 10, 2013 18:37
-
-
Save imduffy15/4504610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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