简体   繁体   中英

How to put/get values into/from Nested HashMap

I want to create a nested HashMap that will take two keys of type float and give out value of type Integer.

 public static HashMap<Float, HashMap<Float, Integer>> hashX = new HashMap<Float,HashMap<Float, Integer>>();

Is there a simple method of putting/getting the values like an ordinary HashMap ie

  hashX.put(key, value);
  hashX.get(key);

or is it a more complicated method that must be used? I have searched around the web for a solution but am finding it tough to find a solution that applies to me. Any help would be appreciated!

Map<Float, Map<Float, Integer>> map = new HashMap<>();

map.put(.0F, new HashMap(){{put(.0F,0);}});
map.put(.1F, new HashMap(){{put(.1F,1);}});

map.get(.0F).get(.0F);

You have to get() the nested map out of the outer map and call can call put() and get() on it

float x = 1.0F;
HashMap<Float, Integer> innerMap = hashX.get(x);
if (innerMap == null) {
    hashX.put(x, innerMap = new HashMap<>()); // Java version >= 1.7
}
innerMap.put(2.0F, 5);

You can create a wrapper class with a method like this:

public class MyWrapper {
    private Map<Float, Map<Float, Integer>> hashX;
    // ...
    public void doublePut(Float one, Float two, Integer value) {
        if (hashX.get(one) == null) {
            hashX.put(one, new HashMap<Float, Integer>());
        }
      hashX.get(one).put(two, value);
    }
}

Please note that you should use interfaces instead of concrete implementations when you declare your fields. For example it would make easier to refactor HashMap into ConcurrentHashMap if the need arises.

You can do it like this:

HashMap<Float, Integer> h1 = new HashMap<Float, Integer>();
h1.put(1.0f,new Integer(1));
HashMap<Float, Integer> h2 = new HashMap<Float, Integer>();
h2.put(3.0f,new Integer(3));

hashX.put(1.0f, h1);
hashX.put(1.0f, h1);

Try this.

static <K0, K1, V> void put(Map<K0, Map<K1, V>> map, K0 k0, K1 k1, V value) {
    map.computeIfAbsent(k0, x -> new HashMap<>()).put(k1, value);
}

static <K0, K1, V> V get(Map<K0, Map<K1, V>> map, K0 k0, K1 k1) {
    return Optional.ofNullable(map.get(k0)).map(s -> s.get(k1)).orElse(null);
}

and

Map<Float, Map<Float, Float>> map = new HashMap<>();
put(map, 1F, 2F, 3F);
put(map, 4F, 5F, 6F);
System.out.println(map);

output:

{4.0={5.0=6.0}, 1.0={2.0=3.0}}

Or simply

Map<Float, Map<Float, Float>> map = Map.of(
    1F, Map.of(2F, 3F),
    4F, Map.of(5F, 6F));

I want to create a nested HashMap that will take two keys of type float and give out value of type Integer.

You don't need a nested Map for that. If you want to lookup using a composite key, it is better to declare your map to be as such. There isn't a good Pair class in JFK, but you ca use Map.Entry , which is somewhat inconvenient to use but works:

Map<Map<Float, Float>, Integer> map = new ....

See https://stackoverflow.com/a/3110563/18573 for creating Map.Entry instances

package com.Collection;

import java.util.*;

public class India {

    public static void main(String[] args) {
        Set s = new TreeSet();
        s.add("Barshi");
        s.add("Pandharpur");
        s.add("Kurduwadi");
        s.add("Vairag");
        Map<String,Map<String,Map<String,TreeSet>>> map =               
                Map.of("India", Map.of("Maharashtra", Map.of("Solapur", new TreeSet(s))));
                
        System.out.println(map);
        System.out.println(map.get("India").get("Maharashtra").get("Solapur").contains("Barshi"));
    }

}

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