java 如何删除地图中重复的键值对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12761567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove duplicate key-value pairings in a map
提问by nitiger
I'm stuck on how to transfer key-value pairs from map1 into map2 only if each key has a unique value in map1.
只有当每个键在 map1 中都有一个唯一值时,我才知道如何将键值对从 map1 传输到 map2。
Let's say I have the following maps:
假设我有以下地图:
- map1: [1,2] [2,4] [4,4]
- map2: [1,2] [2,4]
- 地图1:[1,2] [2,4] [4,4]
- 地图2:[1,2] [2,4]
I suppose the algorithm would be:
我想算法是:
- Loop through entries in the first map.
- Add a key to map2.
- Add a value to a set which checks against the values of map2
- If the values are duplicate the value doesn't get added to the set and disregard adding its corresponding key to map2.
- 循环遍历第一个地图中的条目。
- 向 map2 添加一个键。
- 将一个值添加到一个检查 map2 值的集合中
- 如果值重复,则该值不会添加到集合中,并且忽略将其相应的键添加到 map2。
Code snippet:
代码片段:
public static <K,V> Map<K,V> unique (Map<K,V> m) {
Map<K,V> newMap = new ArrayMap<K,V>();
//Remember all values in the newMap.
Set<V> holding = new ArraySet<V>(newMap.values());
for (Map.Entry<K, V> graphEntry : m.entries()) {
//not sure.
}
return newMap;
}
Is my idea of how its supposed to be done on the right track? Quite lost here.
我的想法是如何在正确的轨道上完成?在这里迷路了。
回答by Luiggi Mendoza
From a Map<K, V>
create a Map<V, K>
that will add the item if and only if the key is not in the map. Using this Map<V, K>
, recreate your Map<K, V>
.
从Map<K, V>
创建一个Map<V, K>
当且仅当键不在地图中时才会添加项目。使用它Map<V, K>
,重新创建您的Map<K, V>
.
public static <K, V> Map<K, V> createMap(Map<K, V> m) {
Map<K, V> map = new HashMap<K, V>();
Map<V, K> tmpMap = new HashMap<V, K>();
for(Map.Entry<K, V> entry : m.entrySet()) {
if (!tmpMap.containsKey(entry.getValue())) {
tmpMap.put(entry.getValue(), entry.getKey());
}
}
for(Map.Entry<V, K> entry : tmpMap.entrySet()) {
map.put(entry.getValue(), entry.getKey());
}
return map;
}
If you need to keep the preserver order of the data, use LinkedHashMap
instead of HashMap
.
如果您需要保持数据的保存顺序,请使用LinkedHashMap
代替HashMap
。
回答by Rohit Jain
Check out Guava BiMap.. This is what you need..
查看Guava BiMap.. 这就是您所需要的..
Although your problem is solved, you can take a look at the below code, to use Guava
API for what you want to do: -
虽然您的问题已解决,但您可以查看以下代码,以使用Guava
API 执行您想要执行的操作:-
public void removeDuplicateValue() {
Map<Integer, String> existingMap = new HashMap<Integer, String>();
existingMap.put(1, "a");
existingMap.put(2, "b");
// Create a new BiMap
BiMap<Integer, String> biMap = HashBiMap.create();
for (Integer val: existingMap.keySet()) {
// forcePut will add a key-value pair, and overwrite the duplicate value.
biMap.forcePut(val, existingMap.get(val));
}
// Create Inverse Map for newly created BiMap.
BiMap<String, Integer> inverseBiMap = biMap.inverse();
for(String val: inverseBiMap.keySet()) {
System.out.println(val + ":" + biMap.get(val));
}
}
回答by Santhanam
Try this one..
试试这个..
Map<String, String> myMap1 = new TreeMap<String, String>();
myMap1.put("1", "One");
myMap1.put("2", "Two");
myMap1.put("3", "One");
myMap1.put("4", "Three");
myMap1.put("5", "Two");
myMap1.put("6", "Three");
Set<String> mySet = new HashSet<String>();
for (Iterator itr = myMap1.entrySet().iterator(); itr.hasNext();)
{
Map.Entry<String, String> entrySet = (Map.Entry) itr.next();
String value = entrySet.getValue();
if (!mySet.add(value))
{
itr.remove();
}
}
Map<String, String> myMap2 = new TreeMap<String, String>(myMap1);
System.out.println("Result :"+myMap2);
Result :{1=One, 2=Two, 4=Three}
结果:{1=一,2=二,4=三}