java.lang.String 无法转换为 java.util.Map

标签 java map

我是 Java 新手,但不是编程新手,因此作为我的第一个项目,我决定为工作人员创建一个 .txt-.csv 解析器。我阅读了 .txt 文件中的每一行,并将其分成单独的 map ,用于部分、子部分、子子部分和子子部分的内容。然后将每个 map 分配给其上方的 map (更多内容见下文)。我将所有内容打印到它,但当我尝试读取它时,出现以下错误:"java.lang.String cannot be cast to java.util.Map"。该错误仅在代码运行后出现,不会在编译时出现,也不会出现在 NetBeans IDE 中。

我的 map 采用以下形式,每个对象都是它下面的 map :(为什么 Java 不能使这变得简单 -_- 我想要关联数组)

(Map)array=<string,Object>
(Map)subarray=<String,Object>
(Map)subsubarray=<String,Object>
(Map)subsubcontents=<String,String>

可能不是最有效的阅读方式,计划稍后将其转换为递归函数,但这是我的代码,是从我的项目中复制粘贴的。我在发现错误的地方添加了注释。

public static Map<String,Object> array=new HashMap<String,Object>();

/* Code for populating the following Maps and pushing them into array
<String,Object>subarray
<String,Object>subsubarray
<String,String>subsubcontents
*/

Set section=array.entrySet();
Iterator sectionI=section.iterator();
while(sectionI.hasNext()) {
    Map.Entry sectionInfo=(Map.Entry)sectionI.next();
    Map<String,Object> subMap=(Map)sectionInfo.getValue();
    Set subSet=subMap.entrySet();
    Iterator subI=subSet.iterator();
    while(subI.hasNext()) {
            Map.Entry subInfo=(Map.Entry)subI.next();
            Map<String,Object> subsubMap=(Map)subInfo.getValue();
            Set subsubSet=subsubMap.entrySet();
            Iterator subsubI=subsubSet.iterator();
            while(subsubI.hasNext()) {
                    System.out.println("test");
                    Map.Entry subsubInfo=(Map.Entry)subsubI.next();
                    Map<String,Object> subcontentsMap=(Map)subsubInfo.getValue();
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
                    Set subcontentsSet=subcontentsMap.entrySet();
                    Iterator keys=subcontentsSet.iterator();
                    while(keys.hasNext()) {
                        Map.Entry keyMap=(Map.Entry)keys.next();
                    }
                    Iterator values=subcontentsSet.iterator();
                    while(values.hasNext()) {
                        Map.Entry valueMap=(Map.Entry)values.next();
                    }
            }
    }
}

任何帮助将不胜感激。我已经为此苦苦挣扎了几天。

最佳答案

我认为你需要清理你的泛型开始:

Set<Map.Entry<String, Object>> section = array.entrySet();
Iterator<Map.Entry<String, Object>> sectionI = section.iterator();
while (sectionI.hasNext()) {
    Map.Entry<String, Object> sectionInfo = sectionI.next();
    Map<String, Object> subMap = (Map<String, Object>) sectionInfo.getValue(); // is this actually a Map<String, Object>?
    Set<Map.Entry<String, Object>> subSet = subMap.entrySet();
    Iterator<Map.Entry<String, Object>> subI = subSet.iterator();
    while (subI.hasNext()) {
        Map.Entry<String, Object> subInfo = subI.next();
        Map<String, Object> subsubMap = (Map<String, Object>) subInfo.getValue(); // is this actually a Map<String, Object>?
        Set<Map.Entry<String, Object>> subsubSet = subsubMap.entrySet();
        Iterator<Map.Entry<String, Object>> subsubI = subsubSet.iterator();
        while (subsubI.hasNext()) {
            System.out.println("test");
            Map.Entry<String, Object> subsubInfo = subsubI.next();
            Map<String, Object> subcontentsMap = (Map<String, Object>) subsubInfo.getValue(); // somehow a String got in here?
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
            Set<Map.Entry<String, Object>> subcontentsSet = subcontentsMap.entrySet();
            Iterator<Map.Entry<String, Object>> keys = subcontentsSet.iterator();
            while (keys.hasNext()) {
                Map.Entry<String, Object> keyMap = keys.next();
            }
            Iterator<Map.Entry<String, Object>> values = subcontentsSet.iterator();
            while (values.hasNext()) {
                Map.Entry<String, Object> valueMap = values.next();
            }
        }
    }
}

然后,您应该更明确地声明 array :

public static Map<String, Map<String, Map<String, Map<String, String>>>> array = new HashMap<String, Map<String, Map<String, Map<String, String>>>>();

这将确保您将正确的对象放入每个 map 中。你永远无法put一个StringMap<>是预期的,因为它不会编译。这将允许您编写以下代码(无需转换):

final Set<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> section = array.entrySet();
final Iterator<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> sectionI = section.iterator();
while (sectionI.hasNext()) {
    final Entry<String, Map<String, Map<String, Map<String, String>>>> sectionInfo = sectionI.next();
    final Map<String, Map<String, Map<String, String>>> subMap = sectionInfo.getValue();
    final Set<Map.Entry<String, Map<String, Map<String, String>>>> subSet = subMap.entrySet();
    final Iterator<Map.Entry<String, Map<String, Map<String, String>>>> subI = subSet.iterator();
    while (subI.hasNext()) {
        final Map.Entry<String, Map<String, Map<String, String>>> subInfo = subI.next();
        final Map<String, Map<String, String>> subsubMap = subInfo.getValue();
        final Set<Map.Entry<String, Map<String, String>>> subsubSet = subsubMap.entrySet();
        final Iterator<Map.Entry<String, Map<String, String>>> subsubI = subsubSet.iterator();
        while (subsubI.hasNext()) {
            System.out.println("test");
            final Map.Entry<String, Map<String, String>> subsubInfo = subsubI.next();
            final Map<String, String> subcontentsMap = subsubInfo.getValue();
            final Set<Map.Entry<String, String>> subcontentsSet = subcontentsMap.entrySet();
            final Iterator<Map.Entry<String, String>> entries = subcontentsSet.iterator();
            while (entries.hasNext()) {
                final Map.Entry<String, String> entry = entries.next();
            }
        }
    }
}

综上所述,所有这些嵌套的泛型看起来都很丑陋。我建议您创建一些对象来表示您的数据。

关于java.lang.String 无法转换为 java.util.Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8171582/

相关文章:

c++ - 无法将迭代器设置为 std::map 的 std::pair

java - javafx 中每个 .css 文件和行的 "Unknown property"警告

java - ChromeDriver - 在 Selenium WebDriver 自动化上弹出禁用开发者模式扩展

Java JToggleButton 在 While 循环中卡住

java - 我在设置 javac 的类路径时遇到问题

java - Freemarker:如何使用枚举作为键来遍历 map

c# - 反射有什么好处?

php - 使用键在 PHP 中进行数组映射

java - 如何检查 Map 中的键是否以给定的字符串值开头

Java:将 Map 作为函数参数传递