簡體   English   中英

創建一個開放哈希表

[英]Create an Open Hashtable

我是一個全新的人,現在我正在為大學編寫代碼。 我想創建一個開放的哈希表,並編寫了以下代碼:

public class AuDOpenHashTable extends AuDHashTable {

private LinkedList<Contact>[] table;


public AuDOpenHashTable(int capacity) {
    super(capacity);
    this.table = new LinkedList[capacity];
}

@Override
public void insert(Contact c) {
    int position = hash(c.email);
    if (table[position] == null) {
        table[position] = new LinkedList<>();
    }
    table[position].add(c);
}

@Override
public void remove(Contact c) throws NoSuchElementException{
    int position = hash(c.email);

    if(table[position] != null){
        table[position].remove();
    }
    else{
        throw new NoSuchElementException();
    }
}

@Override
public Contact getContact(String email)throws NoSuchElementException{
    int position = hash(email);
    table[position].getContact(email);

    if(table[position] != null){
        return table[position].get(position);
    }
    else{
        throw new NoSuchElementException();
    }
}

}

public abstract class AuDHashTable {

protected int capacity;
public AuDHashTable(int capacity){
    this.capacity = capacity;
}

public abstract void insert(Contact c);

public abstract void remove(Contact c);

public abstract Contact getContact(String email);

protected int hash(String s){
    int hash = 0;

    for(int i = 0; i < s.length(); i++){
        hash += s.charAt(i);
    }

    hash = hash % capacity;
    return hash;
}

public static void main(String[] args) {

    AuDClosedHashTable hashtabelle = new AuDClosedHashTable(3);
    Contact eins = new Contact("hans.peter@web.de");
    Contact zwei = new Contact("selina.meier@gmail.com");
    Contact drei = new Contact("alexander.bauer@gmx.de");

    hashtabelle.insert(eins);
    hashtabelle.insert(zwei);
    hashtabelle.insert(drei);


    System.out.println(hashtabelle.isFull());
    System.out.println(hashtabelle.getIndexOf("hans.peter@web.de"));
    System.out.println(hashtabelle.getIndexOf("selina.meier@gmail.com"));
    System.out.println(hashtabelle.getIndexOf("alexander.bauer@gmx.de"));


    hashtabelle.remove(drei);
    System.out.println(hashtabelle.isFull());
    System.out.println(hashtabelle.getContact("selina.meier@gmail.com"));
    System.out.println(hashtabelle.getContact("hans.peter@web.de"));
    System.out.println(hashtabelle.getContact("alexander.bauer@gmx.de"));

    AuDOpenHashTable hashtabelle = new AuDOpenHashTable(3);
    Contact eins = new Contact("hans.peter@web.de");
    Contact zwei = new Contact("selina.meier@gmail.com");
    Contact drei = new Contact("alexander.bauer@gmx.de");

    hashtabelle.insert(eins);
    hashtabelle.insert(zwei);
    hashtabelle.insert(drei);

    System.out.println(hashtabelle.getContact("selina.meier@gmail.com"));

    hashtabelle.remove(zwei);

    System.out.println(hashtabelle.getContact("selina.meier@gmail.com"));
}

}

所以,我的問題是在“ getContact()”方法中。 如果我想在某個頭寸上顯示一個帳戶,並且該帳戶是該頭寸上的唯一帳戶,那么一切正常。 但是,如果要顯示一個頭部不同而尾部不同的帳戶,則有兩個帳戶,它只會給我一個帳戶(大多數情況下不是正確的帳戶)。 對於這些示例,代碼工作得很好,但是如果我決定選擇其他名稱,有時它也將不起作用。 但是,為了使其復雜,我想聽聽您對如何改進“ getContact”方法的建議。 預先感謝。

哈希函數將告訴您某個項目可以位於哪個存儲桶中,但是您仍然需要檢查存儲桶中的所有項目是否相等。 getContact應該遍歷LinkedList並針對每個聯系人檢查電子郵件,然后僅返回具有匹配電子郵件的聯系人。 remove方法相同。

不同的鍵可以具有相同的哈希碼。 通常在插入時檢測到這種情況,在這種情況下,通常會進行重新哈希處理,某種算法會生成另一個哈希代碼,從而導致另一個可能具有空閑代碼的代碼。 如果不是免費的,它將再次被重新修復。 如果這種情況持續很多,則該表可能已分配給較小的表,應使用較大的表。

檢索信息時,應將索引處的數據與搜索到的關鍵字進行比較。 如果不匹配,請重新哈希(與insert相同的算法),然后重試。 直到找到它或以一個空索引結束為止,在這種情況下,密鑰就不存在了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM