curiousguy
curiousguy

Reputation: 3262

How to convert a String ( ArrayList of HashMap ) to ArrayList

Here is my code to create a HashMap and ArrayList .

HashMap wbsMap = new HashMap();
ArrayList<HashMap<?, String>> list = new ArrayList();
ArrayList<HashMap<?, String>> listChildNew = new ArrayList();

Now I have stored the values correspondent to keys like

wbsMap.put("COMPONENT_"+i, bom.getString("COMPONENT"));
wbsMap.put("COMP_QTY_"+i, bom.getString("COMP_QTY").replaceAll("\\s+",""));
wbsMap.put("COMP_UNIT_"+i, bom.getString("COMP_UNIT"));
wbsMap.put("NS_LEFT_"+i,String.valueOf(NS_LEFT));
listChildNew=generateBOMStructureLatest(wbsMap,bom.getString("COMPONENT"),NS_RIGHT,String.valueOf(i));
    if(listChildNew.size() >0){
        wbsMap.put("NS_CHILD_"+i,listChildNew);
    }
wbsMap.put("NS_RIGHT_"+i,String.valueOf(NS_LEFT));
list.add(wbsMap);

Now the key NS_CHILD_ consist of ArrayList of HashMap that is listChildNew . But it get stored in the wbsMap HashMap as String Object. So I am not able to iterate through the value for key NS_CHILD_ . How to convert it back to the ArrayList of HashMap.

Here is how the list is coming in log file.

[{MATL_DESC_0=Slug for spiral casing, NS_LEFT_0=2, COMP_UNIT_0=PC, NS_RIGHT_0=3, COMP_QTY_0=1, COMPONENT_0=400-110}, 
 {NS_LEFT_1=4, MATL_DESC_1=Flat gasket, COMP_UNIT_1=PC, NS_RIGHT_1=5, COMP_QTY_1=1, COMPONENT_1=400-120}, 
 {MATL_DESC_2=Hexagon head screw M10, COMP_UNIT_2=PC, COMPONENT_2=400-130, NS_LEFT_2=6, NS_RIGHT_2=7, COMP_QTY_2=8}, 
 {COMPONENT_3=400-140, NS_RIGHT_3=15, NS_CHILD_3=[{COMPONENT_3_child=400-141, NS_RIGHT_3_child=10, NS_LEFT_3_child=9, COMP_QTY_3_child=1, MATL_DESC_3_child=Sensor, COMP_UNIT_3_child=PC}, 
                                               {COMPONENT_3_child=400-142, NS_RIGHT_3_child=12, NS_LEFT_3_child=11, COMP_QTY_3_child=1, MATL_DESC_3_child=Display, COMP_UNIT_3_child=PC}, 
                                               {COMPONENT_3_child=400-143, NS_RIGHT_3_child=14, NS_LEFT_3_child=13, COMP_QTY_3_child=1, MATL_DESC_3_child=Casing, COMP_UNIT_3_child=PC}], NS_LEFT_3=8, COMP_QTY_3=1, MATL_DESC_3=Revolution counter, COMP_UNIT_3=PC}, 
{COMPONENT_4=400-150, NS_LEFT_4=16, NS_RIGHT_4=23, NS_CHILD_4=[{NS_LEFT_4_child=17, COMPONENT_4_child=400-151, MATL_DESC_4_child=Temperature sensor, NS_RIGHT_4_child=18, COMP_QTY_4_child=1, COMP_UNIT_4_child=PC}, 
                                                            {NS_LEFT_4_child=19, COMPONENT_4_child=400-152, MATL_DESC_4_child=Display, NS_RIGHT_4_child=20, COMP_QTY_4_child=1, COMP_UNIT_4_child=PC}, 
                                                            {NS_LEFT_4_child=21, COMPONENT_4_child=400-153, MATL_DESC_4_child=Casing, NS_RIGHT_4_child=22, COMP_QTY_4_child=1, COMP_UNIT_4_child=PC}], COMP_QTY_4=1, MATL_DESC_4=Thermostat, COMP_UNIT_4=PC}, 
]

Upvotes: 0

Views: 608

Answers (2)

B. Kemmer
B. Kemmer

Reputation: 1537

Your HashMap is of Type , since you put a key and a value as String in it. When you add an Object it will automatically invoke

Object.toString()

You have to serialize your ArrayList!
This can be achieved with e.g. GSON, where you serialize your ArrayList object into a JSON-String.

Now you could deserialize it into a ArrayList again.

Upvotes: 0

George
George

Reputation: 915

It sounds like you're trying to make a tree structure using hashmaps. Try this instead of HashMap.

class HashTree {
    private HashMap<String, HashTree> nodes;
    private String value;

    public HashTree(String value){
        this.value = value;
        this.nodes = new HashMap<>();
    }

    public HashTree(HashMap<String,String> nodes){
        this.value = null;
        this.nodes = new HashMap<>();

        for(Entry<String,String> node : nodes){
            this.nodes.put(node.key,node.value);
        }
    }

    public void put(String key, String value){
        nodes.put(key, new HashTree(value));
    }

    public void put(String key, HashMap<String, String> childNodes){
        nodes.put(key, new HashTree(childNodes));
    }

    // Additional getters and setters
}

Upvotes: 1

Related Questions