-1

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.

Jhern603
  • 11
  • 2
  • 1
    Can you give an example ? Because that is unclear about the String you mentoin at the end. Please edit your post and add an example – azro Oct 26 '20 at 20:46
  • Your question seems to simplify to "How can I pretty print the contents of an Array". Everything else about your HashMap and keys etc seem to be irrelevant. Please read [ask] for overview on how to ask a good question. This question could then be formulated as I suggested and there's probably already answers on SO for that. – Michael Welch Oct 26 '20 at 21:08
  • Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Michael Welch Oct 26 '20 at 21:10
  • Not quite. I'll try to rephrase my question to be clearer. – Jhern603 Oct 26 '20 at 21:13

3 Answers3

2

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.
}
Elmar Brauch
  • 1,286
  • 6
  • 20
0

You can't 'parse' an array, you can only parse strings.

Let's say you know for sure that key hello will map to an int array, then:

int[] v = (int[]) properties.get("hello");
System.out.println("The second value is: " + v[1]);

If you have no idea, you can always do:

Object o = properties.get("hello");
if (o.getClass().isArray()) {
    // we get here if o is an array.
    // (and a NullPointerException if 'hello' is unmapped.

    Object secondValue = java.lang.reflect.Array.get(o, 1);
}

In general your design is bad, though. heterogenerous storage in a map sounds like you very very badly reinvented the concept of a typed object. Just make a class, with a field for each 'property'. If this data is coming from some other system that is not particularly statically/nominally typed (such as, say, a bunch of JSON via a web API) then use tools that are good at dealing with it. For JSON, you'd use jackson or gson, for example.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I considered doing that but wasn't too sure on how to go about it. The reason I'm using an array the way I am is because I have one key value that would store multiple values. As a generic example, say I have a key called "genresOfMusic" and it's values will store a list of different genres (i.e. Rock, Country, Jazz, R&B, etc), would I be able to do that using a class? – Jhern603 Oct 26 '20 at 21:01
  • Sure... `class Song { private final List genres; }`, with `Genre` presumably something like `enum Genre { ROCK, POP, /* etcetera */; }`, or just `List genres` if you just want free strings. Java is a nominally typed language. Make types and give em good names, then java works well. – rzwitserloot Oct 26 '20 at 21:57
0
public static void main(String... args) {
    Map<String, Object> properties = new HashMap<>();
    properties.put("one", new int[] { 1, 2, 3 });
    properties.put("two", new String[] { "4", "5", "6" });

    properties.forEach((key, val) -> {
        if (val == null)
            System.out.format("key: %s is null\n", key);
        else if (val.getClass().isArray()) {
            String[] arr = new String[Array.getLength(val)];

            for (int i = 0; i < arr.length; i++)
                arr[i] = String.valueOf(Array.get(val, i));

            System.out.format("key: %s is an array: %s\n", key, Arrays.toString(arr));
        } else
            System.out.format("key: %s is not an array: %s\n", key, val);
    });
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35