问题是在输出中,单词 smart 被添加了 2 次。你能告诉我为什么使用相同键的重复值被添加两次。在此先感谢您的帮助!
package HashMap;
import java.util.HashMap;
import java.util.Set;
public class Thesaurus {
HashMap<String, String> words =new HashMap<String, String>();
public void add(String x,String y)
{
if (!words.containsKey(x))
words.put(x, y);
else if (!words.containsValue(y))
words.put(x, words.get(x) + " " + y + " ");
}
public void display()
{
System.out.println(words);
}
public static void main(String[] args) {
Thesaurus tc = new Thesaurus();
tc.add("large", "big");
tc.add("large", "humoungus");
tc.add("large", "bulky");
tc.add("large", "broad");
tc.add("large", "heavy");
tc.add("smart", "astute");
tc.add("smart", "clever");
tc.add("smart", "clever");
tc.display();
}
}
输出
{smart=astute clever clever , large=big humoungus bulky broad heavy }