PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How do I return an array that's being stored as a value in a HashMap

 August 17, 2022     arrays, hashmap, java, output, return     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing