简体   繁体   English

搜索哈希表

[英]searching a HashTable

In the following task i have to put names and surnames into a hash table. 在以下任务中,我必须将名称和姓氏放入哈希表中。 The name is a key and the surname is a value. 名称是键,姓是值。 After that i have to enter name's and surname's again. 之后,我必须再次输入姓名和姓氏。 After each input of name and surname i have to check if there is equal of that in the hash table, and if there is equal i have to print something like 'There is equal name and surname'. 在每次输入姓名和姓氏后,我必须检查哈希表中是否相等,如果相等,则必须打印“姓名和姓氏相等”之类的内容。 I have to use data structure's for hashing that our professor gave to us, not the traditional hash imports from Java. 我必须使用我们的教授提供给我们的数据结构进行散列,而不是从Java导入传统的散列。 My problem is that i don't know how to search my Hash table, i have a given search method in the CBTH class which i will put under my code. 我的问题是我不知道如何搜索哈希表,我在CBTH类中有给定的搜索方法,该方法将放在我的代码下。

public class HashLozinki {
public static void main (String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int N = Integer.parseInt(br.readLine());

            CBHT<Korisnici,String> table1 = new CBHT<Korisnici,String>(26);
    for(int i=1;i<=N;i++){
        String imelozinka = br.readLine();
        String[] pom = imelozinka.split(" ");
                    table1.insert(new Korisnici(pom[0]),  new String(pom[1]));
    }

     System.out.println(table1);

     for(int i=1; i<=N; i++){
         String korisnik = br.readLine();
         String[] res = korisnik.split(" ");
         table1.search(res[0]); // Here is my problem :S don't know how to use search
     }

}
}

// The Search Method (part of CBTH class).. i don't know how to implement it
public SLLNode<MapEntry<K,E>> search(K targetKey) {
    // Find which if any node of this CBHT contains an entry whose key is
    // equal
    // to targetKey. Return a link to that node (or null if there is none).
    int b = hash(targetKey);
    for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
        if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
            return curr;
    }
    return null;
}

Class SLLNode must have a method for returning a value (or MapEntry ). SLLNode类必须具有用于返回值(或MapEntry )的方法。

I have found implementation of SLLNode here . 在这里找到了SLLNode的实现。 Unfortunately, the class SLLNode doesn't have any public methods/fields so you should add your class to the same package (or same file). 不幸的是,类SLLNode没有任何公共方法/字段,因此您应将您的类添加到相同的程序包(或相同的文件)中。 You can get value over the chain calls: 您可以通过链式通话获得价值:

table1.search(res[0]).element.value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM