1

Title says all.

Sample code:

ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

HashMap<String, Object> parentHash = new HashMap<String, Object>();
HashMap<String, String> childHash = new HashMap<String, String>();

childHash.put("child_id", "id")
childHash.put("name", "first last");
childHash.put("sex", "man");

parentHash.put("parent_id", "id");
parentHash.put("name", "first last");
parentHash.put("sex", "woman");

parentHash.put("children", childHash);
data.add(parentHash);

Everything looks okay if I print the ArrayList "data" on the screen (example):

[{parent_id=id, name=first last, sex=woman, children=[{
        child_id=id, name=first last, sex=man
    }]
}, {parent_id=id, name=first last, sex=woman, children=[{
        child_id=id, name=first last, sex=man
    }]
}];

So it's HashMap in HashMap and then in the ArrayList. I know how to retrieve value from the parent, but how do I retrieve value from the child?

Aerial
  • 1,185
  • 4
  • 20
  • 42

3 Answers3

4
Map<String, String> childData= (Map<String, String>) parent.get("children");
String childId= childData.get("child_id");

Also note that with your current structure, you can only add one child to a parent:

parentHash.put("children", childHash);
parentHash.put("children", anotherChildHash); //overrides childHash
Map<String, String> childData = (Map<String, String>) parent.get("children");

At the end of the code above, childData contains anotherChildHash and childHash is not stored in the parentHash any longer.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • in parentHash, you store strings and HashMaps - if you retrieve a String and cast it to HashMap, you would get that kind of error (by doing `(Map) parent.get("name");` for example). – assylias Apr 03 '12 at 15:00
  • What do you suggest? Because there are more parents, I use the following code: HashMap fetchChildContent = (HashMap) parentHash.get(counter).get("children"); Any help would be appreciated, because I'm new to Java. – Aerial Apr 03 '12 at 15:13
  • If you use the code you posted in your question with the code I posted, it will work. So you have most likely added something else that does not do what you expect. I suggest you update your question or create a new question with the new code. – assylias Apr 03 '12 at 15:16
  • I'll make a new post, thanx for your support. – Aerial Apr 03 '12 at 15:20
3

The above answer will do want you want (though you'd need to check for childHash being null). However, have you considered defining your own classes? E.g.

public class Person {
    private int person_id;
    private String name;
    private String sex;

    // Handle zero to many children
    private ArrayList<Person> children = new ArrayList<Person>();

    // getters and setters follow.
}

Then later;

if ( parent.hasChildren() ) {
    String name = parent.getChildren().get(0).getName();
}
Rory Hunter
  • 3,425
  • 1
  • 14
  • 16
1

Answer from assylias is correct. I want to add however that you could push your OO design a bit in order to encapsulate all this properly in classes. You will thus avoid to write hard-to-understand-hard-to-maintain code.

Community
  • 1
  • 1
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • Thanx, I made a sample code so that many people can understand. I'll make a class of it (soon). – Aerial Apr 03 '12 at 13:11