0

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}, 
]
9
  • What is bom in your code? Commented May 12, 2015 at 10:52
  • Did u try to define the hashmap generic style?: HashMap<String, ArrayList<HashMap<?, String>>> wbsMap = new HashMap<String, ArrayList<HashMap<?, String>>>(); Commented May 12, 2015 at 10:57
  • bom is SAP JCO JCoTable object JCoTable bom = tables.getTable("T_STPO"); . Commented May 12, 2015 at 10:58
  • @WvdL Yes , but I am getting error . The method put(String, ArrayList<HashMap<?,String>>) in the type HashMap<String,ArrayList<HashMap<?,String>>> is not applicable for the arguments (String, String) . Because I have stored String for other keys Commented May 12, 2015 at 11:02
  • Hm.. what is the error you are getting with your code shown above? Commented May 12, 2015 at 11:05

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.