Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.String cannot be cast to java.util.Map

Tags:

java

map

I'm new to Java, but not new to programming, so as my first project I decided to create a .txt-.csv parser for someone at work. I read each line in the .txt file and separate it into separate Maps for sections, subsections, subsubsections, and the subsubsections' contents. Each Map is then assigned to the Map above it (more on this below). I print everything to it just fine, but when I try to read it I get the following error: "java.lang.String cannot be cast to java.util.Map". The error only appears after the code is run, not while compiling, nor in NetBeans IDE.

My Maps are in the following form with each Object being the Map below it: (Why can't Java make this easy -_- Associative Arrays are all I want)

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

May not be the most efficient way to read this, plan on converting this to recursive function later, but here is my code, copy-pasted from my project. I put comments at where I've found the error to be.

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();
                    }
            }
    }
}

Any help would be much appreciated. I've been struggling with this for a couple of days now.

like image 748
mseancole Avatar asked Dec 27 '22 11:12

mseancole


1 Answers

I think you need to clean up your generics to start with:

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();
            }
        }
    }
}

Then, you should be more explicit with your declaration of array:

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

This would ensure that you are putting the correct objects into each of the maps. You will never be able to put a String value where a Map<> is expected because it will not compile. This will allow you to write the following code (without needing casts):

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();
            }
        }
    }
}

All that being said, all of those nested generics look ugly. I'd recommend you create some objects to represent your data.

like image 69
laz Avatar answered Jan 10 '23 20:01

laz