2

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

asked Oct 23, 2014 at 20:13
2
  • 1
    You would need to provide 3 keys at least. Commented Oct 23, 2014 at 20:14
  • 2
    outerHashmap.get(key1).get(key2).put(key3, value); Commented Oct 23, 2014 at 20:15

5 Answers 5

6

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));
answered Oct 24, 2014 at 17:16
5

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());
 }
}
answered Jan 12, 2017 at 17:03
1

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.

answered Oct 23, 2014 at 20:18
1

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

answered Oct 23, 2014 at 20:16
0

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.

answered Oct 23, 2014 at 20:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.