【发布时间】:2015-07-15 22:01:23
【问题描述】:
此函数循环遍历字典 (allWords) 并使用 getKey 函数生成密钥。 wordListMap 是一个 HashMap> 所以我需要循环并放置键和一个列表。如果没有列表,我放一个,如果有,我只需要附加下一个字典单词。这是我需要帮助的地方。我只是无法弄清楚将下一个单词简单地附加到已经存在的列表中的语法。任何帮助,将不胜感激。
public static void constructWordListMap() {
wordListMap = new HashMap<>();
for (String w : allWords) {
int key = getKey(w);
if (isValidWord(w) && !wordListMap.containsKey(key)) {
List list = new ArrayList();
list.add(w);
wordListMap.put(key, list);
} else if (isValidWord(w) && wordListMap.containsKey(key)) {
wordListMap.put(key, wordListMap.get(key).add(w));
}
}
}
【问题讨论】:
标签: java string dictionary arraylist hashmap