嵌套哈希图覆盖其他键
Nested Hashmaps overwriting other keys
我遇到一个问题,当我想更改嵌套 HashMap 中的值时,其他 HashMap 被覆盖。
例如,我有一个名为 Alligator 的键,它存储一个包含键 Weight 和 Size 的 HashMap。我希望能够更改与 Size 关联的值,但根本不影响 Weight。在 yml 文件中,这就是它的样子。
Alligator:
Weight: 100.0
Size: 10.0
这就是我希望发生的事情。
Alligator:
Weight: 150.0
Size: 10.0
这是我之前找到的一些代码,可以让我更改值,但会覆盖...
HashMap< String, HashMap< String, Double>> data = new HashMap<>();
data.put("Alligator", new HashMap() {
{
put("Size", 10.0
}
});
HashMap< String, HashMap< String, Double>> data = new HashMap<>();
data.put("Alligator", new HashMap() {
{
put("Weight", 100.0
}
});
data.put("Alligator", new HashMap() {
{
put("Weight", 100.0);
}
});
将覆盖 "Alligator" 键的内部映射,如果它已经在外部映射中。
你必须先检查是否存在:
Map<String, Double> inner = data.get("Alligator");
if (inner != null) {
inner.put("Weight", 100.0);
} else {
data.put ("Alligator", new HashMap() {
{
put("Weight", 100.0);
}
});
}
顺便说一句,我不会为内部 Map 使用 HashMap
实例的匿名子 class。
您可以用这个更简单的代码替换它:
Map<String, Double> inner = data.get("Alligator");
if (inner == null) {
inner = new HashMap<>();
data.put ("Alligator",inner);
}
inner.put("Weight", 100.0);
一个
new HashMap(){ // blablabla
表示内存中有一个新的Object实例,
还有一个
data.put("Alligator", new HashMap() { // blablabla
表示在数据映射中添加(key不存在)或覆盖(key存在)一个KV对
正如@Eran 所建议的那样,
inner = data.get("Alligator")
检查内部是否存在
如果你的数据 HashMap
中没有 "Alligator",首先处理你想要发生的事情,可能为 "Alligator" 放入一个新的 HashMap
:
if (!data.containsKey("Alligator")) {
data.put("Alligator", new HashMap<>());
}
然后使用get
获取嵌套的HashMap并使用put
更改值:
HashMap<String, Double> alligator = data.get("Alligator");
alligator.put("Weight", 150.0);
创建一个具有不同名称的内部 HashMap,这样您就不会混淆两个映射。
Map<String,Map<String,Integer>> outermap = new HashMap<String,Map<String,Integer>>();
Map<String,Integer> innermap1 = new HashMap<String,Integer>();
innermap1.put("Weight",100);
innermap1.put("Size", 10);
outermap.put("Alligator",innermap1);
我遇到一个问题,当我想更改嵌套 HashMap 中的值时,其他 HashMap 被覆盖。
例如,我有一个名为 Alligator 的键,它存储一个包含键 Weight 和 Size 的 HashMap。我希望能够更改与 Size 关联的值,但根本不影响 Weight。在 yml 文件中,这就是它的样子。
Alligator:
Weight: 100.0
Size: 10.0
这就是我希望发生的事情。
Alligator:
Weight: 150.0
Size: 10.0
这是我之前找到的一些代码,可以让我更改值,但会覆盖...
HashMap< String, HashMap< String, Double>> data = new HashMap<>();
data.put("Alligator", new HashMap() {
{
put("Size", 10.0
}
});
HashMap< String, HashMap< String, Double>> data = new HashMap<>();
data.put("Alligator", new HashMap() {
{
put("Weight", 100.0
}
});
data.put("Alligator", new HashMap() {
{
put("Weight", 100.0);
}
});
将覆盖 "Alligator" 键的内部映射,如果它已经在外部映射中。
你必须先检查是否存在:
Map<String, Double> inner = data.get("Alligator");
if (inner != null) {
inner.put("Weight", 100.0);
} else {
data.put ("Alligator", new HashMap() {
{
put("Weight", 100.0);
}
});
}
顺便说一句,我不会为内部 Map 使用 HashMap
实例的匿名子 class。
您可以用这个更简单的代码替换它:
Map<String, Double> inner = data.get("Alligator");
if (inner == null) {
inner = new HashMap<>();
data.put ("Alligator",inner);
}
inner.put("Weight", 100.0);
一个
new HashMap(){ // blablabla
表示内存中有一个新的Object实例,
还有一个
data.put("Alligator", new HashMap() { // blablabla
表示在数据映射中添加(key不存在)或覆盖(key存在)一个KV对
正如@Eran 所建议的那样,
inner = data.get("Alligator")
检查内部是否存在
如果你的数据 HashMap
中没有 "Alligator",首先处理你想要发生的事情,可能为 "Alligator" 放入一个新的 HashMap
:
if (!data.containsKey("Alligator")) {
data.put("Alligator", new HashMap<>());
}
然后使用get
获取嵌套的HashMap并使用put
更改值:
HashMap<String, Double> alligator = data.get("Alligator");
alligator.put("Weight", 150.0);
创建一个具有不同名称的内部 HashMap,这样您就不会混淆两个映射。
Map<String,Map<String,Integer>> outermap = new HashMap<String,Map<String,Integer>>();
Map<String,Integer> innermap1 = new HashMap<String,Integer>();
innermap1.put("Weight",100);
innermap1.put("Size", 10);
outermap.put("Alligator",innermap1);