简体   繁体   中英

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. 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.

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 ).

I have found implementation of SLLNode here . Unfortunately, the class SLLNode doesn't have any public methods/fields so you should add your class to the same package (or same file). You can get value over the chain calls:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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