如何修改hashmap (Java)中的key?
How to modify key in hashmap (Java)?
我想更改哈希图中的键。我正在从另一个 hashmap 制作一个 hashmap。
我基本上是在获取一个 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;
}
这可以通过在现有地图中重新映射键并重新收集新地图来实现:
private Map<MetricName, Map<String, Integer>> getMetric(String regionId, Map<String, String> locationMap) {
Map<MetricName, Map<String, Integer>> metricTargetsMap = analyzeMetaService.getMetricTargetsForRegion(regionId);
return metricTargetsMap
.entrySet()
.stream() // stream of Map.Entry<MetricName, Map<String, Integer>>
.map(top -> Map.entry(
top.getKey(), // MetricName
top.getValue().entrySet()
.stream() // stream for inner map Map.Entry<String, Integer>
.collect(Collectors.toMap(
e -> locationMap.get(e.getKey()), // remapped key
e -> e.getValue(), // existing value
(v1, v2) -> v1) // merge function to resolve possible conflicts
)
))
.collect(Collectors.toMap(
Map.Entry::getKey, // MetricName
Map.Entry::getValue // updated map <String, Integer>
));
}
您不要更改密钥。您 (a) 使用旧密钥删除项目,并且 (b) 在新密钥下插入项目。
或者,如果您正在制作一张新地图,基本上是
entry = oldMap.get(oldKey);
newKey = ….whatever...;
newMap.put(newKey, entry);
在幕后,键上的某些功能用作在地图中定位条目的机制。因此,如果您能够更改密钥,“某些功能”将不再将您带到应该找到该条目的地方。
我想更改哈希图中的键。我正在从另一个 hashmap 制作一个 hashmap。
我基本上是在获取一个 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;
}
这可以通过在现有地图中重新映射键并重新收集新地图来实现:
private Map<MetricName, Map<String, Integer>> getMetric(String regionId, Map<String, String> locationMap) {
Map<MetricName, Map<String, Integer>> metricTargetsMap = analyzeMetaService.getMetricTargetsForRegion(regionId);
return metricTargetsMap
.entrySet()
.stream() // stream of Map.Entry<MetricName, Map<String, Integer>>
.map(top -> Map.entry(
top.getKey(), // MetricName
top.getValue().entrySet()
.stream() // stream for inner map Map.Entry<String, Integer>
.collect(Collectors.toMap(
e -> locationMap.get(e.getKey()), // remapped key
e -> e.getValue(), // existing value
(v1, v2) -> v1) // merge function to resolve possible conflicts
)
))
.collect(Collectors.toMap(
Map.Entry::getKey, // MetricName
Map.Entry::getValue // updated map <String, Integer>
));
}
您不要更改密钥。您 (a) 使用旧密钥删除项目,并且 (b) 在新密钥下插入项目。
或者,如果您正在制作一张新地图,基本上是
entry = oldMap.get(oldKey);
newKey = ….whatever...;
newMap.put(newKey, entry);
在幕后,键上的某些功能用作在地图中定位条目的机制。因此,如果您能够更改密钥,“某些功能”将不再将您带到应该找到该条目的地方。