Definity
Definity

Reputation: 722

Accessing Deeply nested HashMaps in Java

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

Upvotes: 2

Views: 12085

Answers (5)

Matt
Matt

Reputation: 1244

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());
    }
}

Upvotes: 5

matt
matt

Reputation: 12347

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));

Upvotes: 6

M A
M A

Reputation: 72844

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

Upvotes: 1

christopher
christopher

Reputation: 27346

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.

Upvotes: 1

talnicolas
talnicolas

Reputation: 14053

So first you will do a get on the first HashMap with a String as key. It will return you an HashMap<Float,HashMap<Float,String>>.

You will then do a get on that HashMap with a Float as key. It will return a HashMap<Float,String>.

You will finally do a get on that HashMap with a Float as key and there, you have the String you want. Same thing with a put instead of get on the last HashMap to insert a value.

Upvotes: 0

Related Questions