【发布时间】:2015-04-03 22:42:22
【问题描述】:
我有一个哈希图,其中包含学生 ID 作为键和一些字符串作为值。 它包含像
这样的数据a abc.txt
b cde.txt
d abc.txt
我想在地图中找到重复的值并将它们替换为流派值。我想要一张类似的地图
a abc.txt
b cde.txt
d abc_replacevalue.txt
我已经尝试了代码,但它不起作用
Map<String,String> filemap = new HashMap<String,String>();
// filemap is the original hash map..
Set<String> seenValues = new HashSet<String>();
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<String, String> entry : filemap.entrySet()) {
String value = entry.getValue();
if (seenValues.contains(value)) {
value = "updated"; // update value here
}
result.put(entry.getKey(), value);
seenValues.add(value);
}
for (String key : result.keySet() ) {
String value = result.get( key );
System.out.println(key + " = " + value);
}
输出还是一样
a abc.txt
b cde.txt
d abc.txt
【问题讨论】: