java class 扩展 HashMap <String, Map <String, List<String> > 覆盖方法 put 不起作用

java class that extend HashMap <String, Map <String, List<String> > overwrite method put dont work

嗨,我的 class:

有问题
public class JarClassMethod extends HashMap<String,HashMap<String, List<String>>> {
    public JarClassMethod() {
        super();
    }

    public List<String> get(String jar,String classe) {
        return get(jar).get(classe);
    }

    public void put(String jar, String classe, List<String> metodi) {

        Map<String,List<String>> map2 = new HashMap<>();
        map2.put(classe, metodi);
        this.put(jar, (HashMap<String, List<String>>) map2);
    }

}

如果调用放在 for 循环中,我最后只查看 MAP 对象中的最后一个值!

示例:

{"jar1":{classe1:[s,adas,da,sd],
         classe2:[asda,fdd,ass],
        },
"jar2":{classe3:[s,adas,da,sd],
         classe4:[asda,fdd,ass],
        }
}

谢谢 问候

如果您为现有键设置新值,Map 会覆盖以前的值。检查 documentation 其中指出:

If the map previously contained a mapping for the key, the old value is replaced by the specified value.

如果您总是使用相同的键调用 put,请检查您的循环。如果是,最后,Map 将只有一个值,即最后输入的值。

确保创建一个由 map2 引用的新对象,如下所示:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Testing {
    public static void main(String[] args) {
        HashMap<String,HashMap<String, List<String>>> map=new HashMap<String,HashMap<String, List<String>>>();
        Map<String,List<String>> map2;      

        map2= new HashMap<>();      
        map2.put("classe1",List.of("s","adas","da","sd"));
        map2.put("classe2",List.of("asda","fdd","ass"));
        map.put("jar1", (HashMap<String, List<String>>) map2);      
        System.out.println(map);

        map2= new HashMap<>();
        map2.put("classe3",List.of("s","adas","da","sd"));
        map2.put("classe4",List.of("asda","fdd","ass"));
        map.put("jar2", (HashMap<String, List<String>>) map2);
        System.out.println(map);
    }
}

输出:

{jar1={classe2=[asda, fdd, ass], classe1=[s, adas, da, sd]}}
{jar2={classe4=[asda, fdd, ass], classe3=[s, adas, da, sd]}, jar1={classe2=[asda, fdd, ass], classe1=[s, adas, da, sd]}}

您的 put 方法正在替换给定 jar 键的内部映射,因此只会保留给定 jar 键的最后一个 classe。当 jar 键还没有时,您应该只放置一个新的内部 HashMap

方法实现应该是:

public void put(String jar, String classe, List<String> metodi) {
    computeIfAbsent(jar, k -> new HashMap<>()).put(classe, metodi);
}

此外,为了防止 NullPointerException,您的 get 方法应该是:

public List<String> get(String jar, String classe) {
    HashMap<String, List<String>> classeMap = get(jar);
    return (classeMap == null ? null : classeMap.get(classe));
}

测试

JarClassMethod jcm = new JarClassMethod();
jcm.put("jar1", "classe1", Arrays.asList("s","adas","da","sd"));
jcm.put("jar1", "classe2", Arrays.asList("asda","fdd","ass"));
jcm.put("jar2", "classe3", Arrays.asList("s","adas","da","sd"));
jcm.put("jar2", "classe4", Arrays.asList("asda","fdd","ass"));
jcm.forEach((k,v) -> System.out.println(k + "=" + v));

输出

jar2={classe4=[asda, fdd, ass], classe3=[s, adas, da, sd]}
jar1={classe2=[asda, fdd, ass], classe1=[s, adas, da, sd]}