【发布时间】:2020-11-17 07:27:49
【问题描述】:
我想更改哈希图中的键。我正在从另一个哈希图制作哈希图。
我基本上是得到一个 id 并返回一个名字。
所以基本上我得到的是:
'BOS': 300
但我想得到:
'Boston':300
private Map<MetricName, Map<String, Integer>> getMetric(String regionId, Map<String, String> locationMap){
Map<MetricName, Map<String, Integer>> metricTargetsMap = analyzeMetaService
.getMetricTargetsForRegion(regionId);
Map<MetricName, Map<String, Integer>> metricTargetsMapModified = new HashMap<MetricName, Map<String, Integer>>();
metricTargetsMap.forEach((metricName,targetMap)-> {
HashMap<String, Integer> modifiedMap = new HashMap<String, Integer>();
targetMap.forEach((location, targetValue) -> modifiedMap.put(locationMap.get(location), targetValue));
metricTargetsMapModified.put(metricName, modifiedMap);
}
);
return metricTargetsMapModified;
}
【问题讨论】:
-
“分配数据类型”是什么意思?
-
你会喜欢从 Java 10 开始的
var。 -
请添加样本输入数据和对应的预期输出
-
INT 中的数据类型
-
为什么您需要修改哈希映射中的键?通常认为bad practice 使用可变键,因为它可能会产生不良的副作用。如果您的意思是
renaming键,则应删除旧键并将值与新键放在一起。