java - 创建通用哈希表时出错

标签 java collections hashtable

我在尝试创建通用哈希表时遇到以下错误。

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/

相关文章:

node.js - NodeJS + Mongo native——查询前判断集合是否存在

python - 二次复杂度的 4SUM 变化(Python 3.5)

hashtable - 一致性哈希 : Where is the data-structure of ring kept

java - 如何使用 Selenium WebDriver 从变量中提取文本?

java - 通过 PDFBox 从 PDF 文件中提取时具有黑色背景(或全白)的 Jpg

java - 在Java中获取日期范围内的所有星期五

javascript - 在 Backbone.js 集合中制作一个模型 'selected' 的最佳方法?

java - Java 中的表可以使用什么数据结构?

java - 优雅地编写初始化的静态哈希表

java - 将 Clickable JPanel 添加到 AbstractTableModel