我尝试更新我的 Java 技能。但现在我卡在了一个哈希图上。我没有得到正确的对象。这是我的示例代码:
public class Sample {
private static Map<String, Map<String, String>> peaks = new HashMap<String, Map<String, String>>();
private final String name;
public Sample(String name) {
this.name = name;
this.peaks = new HashMap<String, Map<String, String>>();
}
public static Map<String, Map<String, String>> getPeaks() {
return peaks;}
public static void addPeak(String peakName, String value) {
Map<String, String> peak = new HashMap<String, String>();
peak.put("value", value);
peaks.put(peakName, peak);
}
}
public class Main {
static Map<Integer, Sample> sample = new HashMap<Integer, Sample>();
public static void main(String[] args) {
Sample sam = new Sample("Test1");
sample.put(1, sam);
sample.get(1).addPeak("A", "1");
sam = new Sample("Test2");
sample.put(2, sam);
sample.get(2).addPeak("B", "123");
System.out.println(sample.get(1).getPeaks().toString());
System.out.println(sample.get(2).getPeaks().toString());
System.out.println(sample.get(4).getPeaks().toString());
}
}
每次输出都是:{B={value=123}}
我不知道哪一部分是错的。我错过了什么?