简体   繁体   English

如何在嵌套 HashMap 中放入/获取值

[英]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.我想创建一个嵌套的 HashMap,它将采用两个 float 类型的键并给出 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是否有像普通 HashMap 那样放置/获取值的简单方法,即

  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您必须从外部映射中get()嵌套映射,并且调用可以在其上调用put()get()

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.例如,如果需要,将HashMap重构为ConcurrentHashMap会更容易。

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.我想创建一个嵌套的 HashMap,它将采用两个 float 类型的键并给出 Integer 类型的值。

You don't need a nested Map for that.不需要嵌套 Map 。 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: JFK 中没有好的Pair类,但您可以使用Map.Entry ,这有点不方便使用但有效:

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

See https://stackoverflow.com/a/3110563/18573 for creating Map.Entry instances有关创建Map.Entry实例的信息,请参阅https://stackoverflow.com/a/3110563/18573

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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