当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to connect between key and value in a hash table?

问题描述:

I learning about hash tables from "data structres and algorithems in java" and there is a concept I couldn't understand. for example, I want to build an phone book. I want a hashtable which gets name of person as a key and spit back his phone. I understood how to make an hash function to create from the person's name a numerical index. But after I inserted the name of the person to the hash table by his numerical index, how can I get his phone? how can I connect between this keys and values?

edit:

Im trying to implementing my own hash-table and not use the java api

网友答案:

You should store a key-value pair in the hash table, not just key. The setter methods accept key/value pair, wrap them into key/value pair instance and store in the bucket. Access methods calculate bucket index based on key and iterate through pairs looking for the one having exact key. May be class Map.Entry can give you some ideas.

分享给朋友:
您可能感兴趣的文章:
随机阅读: