자바에서 해시 테이블 용 클래스를 작성하고 있습니다 ... 지금까지 제대로 수행하고 있는지 확인하십시오.HashTable Java ... 내 코드를 확인할 수 있습니까
은 내가 오랫동안 유형 인 학생의 ID를 기반으로 해시 값 ...
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);
}
}
}
이 숙제가 있습니까? 그렇다면이 질문에 그처럼 태그를 붙여야합니다. – Seb
자바에는 .equals (Object) 및 .hashCode() 메소드를 포함하는 [계약] [1]이 있으며 키의 유형과 독립적으로 해시 테이블을 구현할 수 있습니다. [1] : http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html – wowest