我在尝试创建通用哈希表时遇到以下错误。
The method put(String, capture#2-of ?) in the type Hashtable<String,capture#2-of ?> is not applicable for the arguments (String, int)
我需要一个哈希表,如下所示:
HashTable<String, HashTable<String, AnyWrapperType>>
by AnyWrapperType i mean it could be Integer or String or Character or Boolean
我用过?
对于 AnyWrapperType
,但我收到一个错误。
我也关注了link从那里我知道我做错了什么,但可以找到出路。
Hashtable<String, Hashtable<String, String>> paramTocol = new Hashtable<>();
if (standard != null && (!standard.isEmpty())) {
Hashtable<String, String> temp = new Hashtable<>();
temp.put(Appconstants.Col_studentinfo_S_Class,
AvailableClass.getValueByName(standard));
paramTocol.put("standard", temp);
}
if (section != null && (!section.isEmpty())) {
Hashtable<String, String> temp = new Hashtable<>();
temp.put(Appconstants.Col_studentinfo_S_Section, section);
paramTocol.put("section", temp);
}
此处standard is Integer
的实际值在数据库中,值对应于 section is char
。我将所有这些值作为字符串给出,我需要将它们存储在哈希表中,所以我想要一些原始哈希表来保存所有这些值。没有任何类型是用户定义的
最佳答案
我认为您可以将AnyWrapperType
设置为Object
。由于java中的所有类都是Object的直接或间接子类,因此您可以将类型设置为Object,而不是设置泛型类型。
当您需要获取该元素时,您应该执行类型转换
。
HashTable<String, HashTable<String, Object>>
关于java - 创建通用哈希表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17305300/