简体   繁体   中英

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

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

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

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM