【发布时间】:2016-02-22 13:13:21
【问题描述】:
我创建了一个哈希表,其中一个字符串作为键,一个字符串的 LinkedList 作为我的值。这是我的实现:
Hashtable <String, LinkedList<String>> table = new Hashtable <String, LinkedList<String>>();
我想要做的是对一个单词文件进行排序,并将每个排序后的单词存储到哈希表中(代表键),并将 ORIGINAL 单词存储为我的 LinkedList 值的一部分。
例如,如果单词是
"cat"
Sorted = "act"
Store in Hashtable (key : act, value : cat);
现在我只是对如何从本质上添加到我的 LinkedList 感到困惑。
这就是我的想法:
LinkedList <String> temp = table.get(sortedWord) //if null, then nothing in list
if(temp==null)
table.put(sortedWord,temp.add(originalWord));
This is not working since its not following the library functions but I'm unsure of how I would do this.
【问题讨论】:
-
如果你的值是单值,那么你可以直接使用 Hashtable
table = new Hashtable (); -
但是如果文本文件包含多个排序后相同的单词怎么办?这就是为什么我想要一个 LinkedList 作为值
-
仅供参考,我们通常使用
HashMap而不是Hashtable,除非这是一个多线程程序。见docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html。