I have a hashmap declared as :
HashMap<String, Double> hm = new HashMap<String, Double>();
我正在声明一个向量:
Vector<String> productList = new Vector<String>();
现在,我正在尝试将键添加到向量中:
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
//System.out.print(me.getKey() + ": ");
productList.add(me.getKey());
//System.out.println(me.getValue());
}
//hm is the HashMap holding the keys and values.
当我编译代码时,它会出现以下错误:
ProductHandler.java:52: error: no suitable method found for add(Object)
productList.add(me.getKey());
^
method Collection.add(String) is not applicable
(argument mismatch; Object cannot be converted to String)
我们在尝试将值添加到向量之前,需要将其转换为字符串类型吗?我有什么遗漏的吗?
Vector<String> productList = new Vector<String>(hm.keySet());
。 - αƞjiβ