我必须使用散列表创建anagram字典。我从用户那里接收一个单词,并且必须从我的字典中输出该单词的所有字母。制作anagram字典
这是我目前的程序,我创建了一个散列函数来计算每个单词的散列值,并且eachother的字母散列值将具有相同的散列值,并放置在散列表中的同一个插槽中。
我遇到困难的部分是,当我创建此映射并对用户输入的单词执行散列函数以获取散列表的索引时,如何才能够返回所有那些在该散列表中的值指数? 这是到目前为止我的代码
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
System.out.println("Total file size to read (in bytes) : " + fis.available());
String content = new String();
while ((content = br.readLine()) != null) {
singleAddress.add(content);
}
for(int i = 0; i<singleAddress.size(); i++)
{
char[] chars = singleAddress.get(i).toCharArray();
Arrays.sort(chars);
int hash = 0;
for(int j = 0; j<chars.length; j++)
{
hash = 2*hash + (int)chars[j];
}
numbers.put(singleAddress.get(i), hash);
System.out.println(hash + " " + i);
}
我相信这将创建一个哈希表字谜解释,但我不知道我怎么会定索引处返回所有的值。
没有办法脱身的'HashMap'一组,它的键与给定的键冲突的所有值。众所周知,_collision_的概念是一个内部实现细节。 OTOH,如果你想写你自己的'HashMap'版本,那么,这是另一回事。你可以做你想做的。 –