我正在尝试使用HashTable(我必须使用散列)来存储电话目录。我想根据姓氏(按字母顺序)输入电话条目。我决定使用条目中第一个字符的hashCode作为HashTable的键。
这是我在Entry类中编写的hashCode:
@Override
public int hashCode() {
int index= getSurname().charAt(0);
return index;
}
这是addEntry方法,用于向目录添加新条目:
@Override
public void addEntry(Entry line) {
//Throws an exception if either surname, initials or number are null.
if (line.getSurname()==null||line.getInitial()==null||line.getNumber()==null){
throw new IllegalArgumentException("Please provide a Surname, Initials and a Number");
}
//Throws an exception if initials are less than 2
if (line.getInitial().length() < 2) {
throw new IllegalArgumentException("Please make sure that initials include both first name and surname eg: AK");
}
//Throws an exception if the length of the number is not 5 characters.
if (line.getNumber().length() != 5) {
throw new IllegalArgumentException("Please provide a Number that is 5 characters long (make sure that it starts with 0)");
}
//Throws an exception if the number doesnt start with 0.
if (line.getNumber().startsWith("0") == false) {
throw new IllegalArgumentException("The Number must start with a 0");
}
//Initialises the key using the hashCode of surname.
int key=line.getSurname().hashCode();
//If statement checks if the HashTable entries contains the key
if (entries.contains(key)){
//If it does it creates a linkedList called list
LinkedList<Entry> list =new LinkedList();
list=entries.get(key);
ListIterator<Entry> iterator = list.listIterator();
while (iterator.hasNext()){
int x=0;
String one = list.get(x).getSurname();
if (one.compareToIgnoreCase(line.getSurname()) > 0){
entries.put(x,list);
x++;
break;
}
}
} else {
LinkedList<Entry> list2 =new LinkedList();
list2.add(line);
entries.put(key,list2);
}
}
问题是当我做一些测试以查看添加不同条目的结果时,我发现密钥(hashCode)未正确生成。
这些是结果:
Key: -2083437938. Value: [Entry{surname='Jansas initials=KJ Phone number=05544'}]
Key: -1911680082. Value: [Entry{surname='Paul initials=AP Phone number=05572'}]
Key: 66344. Value: [Entry{surname='Aza initials=AZ Phone number=05212'}]
Key: 867699843. Value: [Entry{surname='Penny initials=KP Phone number=05271'}]
Key: 1953849949. Value: [Entry{surname='Aanais initials=AP Phone number=05562'}]
姓氏“Aza”的条目不应该超过“Penny”而旁边的“Aanais”吗?关于如何解决问题的任何想法?
你写了
...
int key=line.getSurname().hashCode();
...
当我正确地看到它时,line
变量的类型为Entry
,你在hashCode
类中覆盖Entry
。以上剪辑应该是
...
int key=line.hashCode();
...
否则你使用类hashCode
的String
方法(或者getSurname
返回的任何方法)。
但是,这不是Java哈希代码的良好实现,Java哈希代码不是为您的用例设计的。我会重新考虑你在这里用来排序的方法。