哈希码返回意外结果

Hashcode returning unexpected results

我正在尝试使用 HashTable(我必须使用散列)来存储电话簿。我想根据姓氏(按字母顺序)对电话条目进行排序。我决定将条目中姓氏的第一个字符的 hashCode 用作 HashTable 的键。

这是我在条目中写的hashCode class:

@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 并且您覆盖了 Entry class 中的 hashCode。上面的片段应该是

...
int key=line.hashCode();
...

否则你使用 class StringhashCode 方法(或任何 getSurname returns)。

然而,这不是 Java 哈希码的良好实现,它不是为您的用例设计的。我会重新考虑您在这里使用的排序方法。