尝试将字符串与哈希表进行比较 key/value
trying to compare a String to a Hashtable key/value
Hashtable<String, Integer> coordinates= new Hashtable<String, Integer>();
String value = (String) cb1.getSelectedItem();
if(value==coordinates.keys())
它给了我一个 "imcompatible operand types String and Enumeration" 错误,知道如何在没有循环的情况下比较这两个吗?
你不能仅仅因为 Set
是 Strings
的集合。比较就像 "I've got this bucket of apples and I got this apple, let's compare them"。不过,您可以做的是检查 String
是否是 Set
的一部分,这就像调用 coordinates.keys().contains(value)
.
一样简单
关于比较:
不要用 ==
来比较 Strings
。 ==
按值比较,在 Objects
的情况下,例如 Strings
是参考。请改用 string1.equals(string2)
。此主题涵盖更广泛 in this question
使用将 return 集合的 keySet 方法。然后用选定的值调用包含它。
Hashtable<String, Integer> coordinates= new Hashtable<String, Integer>();
String value = (String) cb1.getSelectedItem();
if (coordinates.keySet().contains(value)) {
//Some action...
}
Hashtable<String, Integer> coordinates= new Hashtable<String, Integer>();
String value = (String) cb1.getSelectedItem();
if(value==coordinates.keys())
它给了我一个 "imcompatible operand types String and Enumeration" 错误,知道如何在没有循环的情况下比较这两个吗?
你不能仅仅因为 Set
是 Strings
的集合。比较就像 "I've got this bucket of apples and I got this apple, let's compare them"。不过,您可以做的是检查 String
是否是 Set
的一部分,这就像调用 coordinates.keys().contains(value)
.
关于比较:
不要用 ==
来比较 Strings
。 ==
按值比较,在 Objects
的情况下,例如 Strings
是参考。请改用 string1.equals(string2)
。此主题涵盖更广泛 in this question
使用将 return 集合的 keySet 方法。然后用选定的值调用包含它。
Hashtable<String, Integer> coordinates= new Hashtable<String, Integer>();
String value = (String) cb1.getSelectedItem();
if (coordinates.keySet().contains(value)) {
//Some action...
}