Issue
I am trying to store an array within a hashmap such that the hashmap is also the child of another hashmap.
To visually represent what I mean:
parentHashMap <"myParentKey":childHashMap>
---childHashMap <"properties":myArray[]>
------myArray = ["value 1", "value 2", "etc"]
The reason why I am making this eyesore of a storage solution is that I want my childHashMap to have different "properties" values for each key in parentHashMap.
myArray wouldn't necessarily be all the properties that I am trying to store, rather, it would be one property that can hold multiple values (i.e. <"genresOfMusic" : "rock, metal, jazz, country">)
Ultimately, how would I return myArray so that I can show its contents? Also, suggestions to better format my storage solution would be greatly appreciated instead of having nesting maps.
Solution
If I understand your question like you put a instance of String[] into the map, then:
You have to cast it back to this type. Could be done like this:
HashMap<String, Object> properties = new HashMap<>();
Object array = properties.get("Key_To_Array");
if (array instanceof String[]) {
String[] arrayElementsAsString = (String[]) array;
// Do something with Strings in array.
}
Answered By - Elmar Brauch Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.