Accessing Deeply nested HashMaps in Java

10,578

Solution 1

First create the map:

HashMap<String, HashMap<Float,HashMap<Float, String>>> map = new HashMap<>();

Then put the first level map into it:

map.put("one", new HashMap<Float, HashMap<Float, String>>());

Then put a HashMap in the last level:

map.get("one").put(1.0f,new HashMap<Float, String>());

Now put an element in the new map:

map.get("one").get(1.0f).put(2.0f,"this is lame");

and now it can be gotten as described above:

System.out.println(map.get("one").get(1.0f).get(2.0f));

Solution 2

If you plan on constructing homogeneous HashMaps with variable depth, use a recursive data structure.

Below is an implementation providing a sample interface:

class NestedMap<K, V> {

    private final HashMap<K, NestedMap> child;
    private V value;

    public NestedMap() {
        child = new HashMap<>();
        value = null;
    }

    public boolean hasChild(K k) {
        return this.child.containsKey(k);
    }

    public NestedMap<K, V> getChild(K k) {
        return this.child.get(k);
    }

    public void makeChild(K k) {
        this.child.put(k, new NestedMap());
    }

    public V getValue() {
        return value;
    }

    public void setValue(V v) {
        value = v;
    }
}

and example usage:

class NestedMapIllustration {
    public static void main(String[] args) {

        NestedMap<Character, String> m = new NestedMap<>();

        m.makeChild('f');
        m.getChild('f').makeChild('o');
        m.getChild('f').getChild('o').makeChild('o');
        m.getChild('f').getChild('o').getChild('o').setValue("bar");

        System.out.println(
            "nested element at 'f' -> 'o' -> 'o' is " +
            m.getChild('f').getChild('o').getChild('o').getValue());
    }
}

Solution 3

Let's have a look, shall we?

First layer is a HashMap<String, HashMap>, so let's get.

map.get(strKey); // Returns another HashMap

We've got another HashMap back, so what do we do? We use get again!

map.get(strKey).get(1.0f); // Returns another HashMap

Again, what do we do? Well only one thing for it. get!

map.get(strKey).get(1.0f).get(1.0f); // Returns a String

This is the value in the deeply nested HashMap.

Solution 4

Having HashMap<String,HashMap<Float,HashMap<Float,String>>> map and without accounting for null values, just follow the logical sequence that you would formulate in your mind to access the inner map and translate to the following code:

map.get(strKey).get(floatKey).put(newFloat, newString);
map.get(strKey).get(floatKey).remove(newFloat);

strKey is a key String in the first-level map

floatKey a key Float in the second-level map

Share:
10,578
Definity
Author by

Definity

Updated on July 26, 2022

Comments

  • Definity
    Definity almost 2 years

    So I have this HashMap:

    HashMap<String,HashMap<Float,HashMap<Float,String>>>

    But I'm not to sure how to add and remove elements from the most deeply nest structure. Can someone give an example?

    Thanks :)

    Update:

    Thanks for the help, But how can I just put on the first level of the HashMap? I have tried .put and I am getting an error.

    Thanks