【发布时间】:2020-05-08 03:52:17
【问题描述】:
我在一个类中创建了一个嵌套的 HashMap,如下所示。这里的问题是当我为不同的 CPID 调用函数 XOXO 时,例如Profile1、Profile2 然后 HashMap 只存储最后一个配置文件值,即 Profile2。
但是,很明显,我每次都将内部 HashMap PV 存储到外部 HashMap CP 中以用于不同的键,即 CPID (Profile1, Profile2)
public class abc {
public static HashMap<String, HashMap<String, String>> CP = new HashMap<>();
HashMap<String, String> PV = null;
public void XOXO(CPID) {
PV = new HashMap<>();
String Value1 = Function.getText(5, 5);
String Value2 = Function.getText(6, 6);
//Note value at coordinate (5,5)/(6,6) is changing for diff CPID
PV.put("AB", Value1);
PV.put("CD", Value2);
CP.put(CPID, PV);
}
}
输出:
{Profile1, {AB= 123, CD = 456}
{Profile2, {AB= 123, CD = 456}
预期输出:
{Profile1, {AB= 999, CD = 888}
{Profile2, {AB= 123, CD = 456}
【问题讨论】:
标签: java multidimensional-array hashmap