Java 如何从哈希映射中删除重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21625772/
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 value from hash map
提问by user3283502
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class MyClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1, "Anil");
hm.put(2, "Deven");
hm.put(3, "sanjay");
hm.put(4, "sanjay");
hm.put(5, "Raj");
hm.put(6, "sanjay");
Set<Integer> keys = hm.keySet();
}
}
This my code i want to remove all duplicate value from hash-map and want print on console please tell me how i will do this .
这是我的代码,我想从哈希映射中删除所有重复值并希望在控制台上打印请告诉我我将如何执行此操作。
采纳答案by peter.petrov
Your HashMap is hm
. Put the values of hm
in another HashMap hm2
in which the values of hm
are the keys of hm2
, and the values of hm2
can be anything (e.g. the object Boolean.TRUE).
你的 HashMap 是hm
. 将 的值hm
放在另一个HashMaphm2
中,其中的值hm
是 的键hm2
, 的值hm2
可以是任何东西(例如对象Boolean.TRUE)。
Then loop through that second HashMap hm2
and print out its keys.
然后循环遍历第二个 HashMaphm2
并打印出它的键。
Instead of HashMap
you can also use HashSet
for hm2
(this is even better as you would not need the Boolean.TRUE part).
相反的HashMap
,你也可以使用HashSet
的hm2
(这甚至更好,因为你不会需要Boolean.TRUE部分)。
import java.util.HashMap;
import java.util.HashSet;
public class MyClass {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1, "Anil");
hm.put(2, "Deven");
hm.put(3, "sanjay");
hm.put(4, "sanjay");
hm.put(5, "Raj");
hm.put(6, "sanjay");
HashSet<String> hm2 = new HashSet<String>();
hm2.addAll(hm.values());
for (String str : hm2){
System.out.println(str);
}
}
}
回答by Ankur Shanbhag
It is not possible to keep the mapping along with the key if you want duplicate values to be eliminated, as unique keys associated with duplicate values will also be dropped off from the map.
如果您希望消除重复值,则无法将映射与键一起保留,因为与重复值关联的唯一键也会从映射中删除。
If you just need a list of unique values from the map try this:
如果您只需要地图中的唯一值列表,请尝试以下操作:
Try this:
尝试这个:
Set<String> set = new HashSet<String>(hm.values());
System.out.println(set);
Output: [Anil, Deven, Raj, sanjay]
输出:[阿尼尔、德文、拉吉、桑杰]
回答by Marco13
Invert the map twice
反转地图两次
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class UniqueMapValues
{
public static void main(String[] args)
{
Map<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1, "Anil");
hm.put(2, "Deven");
hm.put(3, "sanjay");
hm.put(4, "sanjay");
hm.put(5, "Raj");
hm.put(6, "sanjay");
hm = invert(invert(hm));
System.out.println(hm);
}
private static <K, V> Map<V, K> invert(Map<K, V> map)
{
Map<V, K> result = new HashMap<V, K>();
for (Entry<K, V> entry : map.entrySet())
{
result.put(entry.getValue(), entry.getKey());
}
return result;
}
}