8
我有一個Color類,我將其放入散列表中。我想打電話containsKey
的HashMap來確保該對象是否已經存在於HashMap中使用自定義類在散列表上調用containsKey
Color類
public class Color {
public String name;
Color (String name) {this.name = name;}
//getters setters for name
}
的HashMap
HashMap<Color, List<String>> m = new HashMap<Color, List<String>>();
Color c = new Color("red");
m.put(c, new ArrayList<String>());
Color c1 = new Color("red");
System.out.println(m.containsKey(c1)); //I'd like to return this as true
由於c1
有name
紅色。我希望System.out
返回true,因爲地圖中已存在的鑰匙c
有name
紅色
這怎麼能實現?