Hi, I'm writing an class for a hash table in java... can you please make sure that I am doing it correctly so far.
I need to store StudentRecord objects in it.... I am calculating the hash value based on the student's ID which is of type long...
package proj3;
import java.util.LinkedList;
public class HashTable {
LinkedList<StudentRecord> [] buckets;
int size;
public HashTable(){
size = 10;
initialize();
}
public HashTable(int initialSize){
size = initialSize;
initialize();
}
private void initialize(){
for(int i=0; i<size; i++){
buckets[i] = new LinkedList<StudentRecord>();
}
}
/** for testing only
private int calculateHashString(String s){
int hash = 0;
for(int i=0; i<s.length(); i++){
hash += s.charAt(i);
}
return hash % size;
}
**/
private int calculateHash(long l){
return (int) (l % size);
}
public boolean contains(StudentRecord sr){
int hash = calculateHash(sr.studentID);
LinkedList<StudentRecord> l = buckets[hash];
if(l.contains(sr)){
return true;
}
return false;
}
public void put(StudentRecord sr){
int hash = calculateHash(sr.studentID);
LinkedList<StudentRecord> l = buckets[hash];
if(!l.contains(sr)){
buckets[hash].add(sr);
}
}
}