从Java中删除HashMap中的重复值
我有一个重复值的地图:
("A", "1"); ("B", "2"); ("C", "2"); ("D", "3"); ("E", "3");
我想要地图了
("A", "1"); ("B", "2"); ("D", "3");
你知道如何摆脱重复的价值吗?
目前,我收到’java.util.ConcurrentModificationException’错误。
谢谢。
public static void main(String[] args) { HashMap map = new HashMap(); map.put("A", "1"); map.put("B", "2"); map.put("C", "2"); map.put("D", "3"); map.put("E", "3"); Set keys = map.keySet(); // The set of keys in the map. Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { String key = keyIter.next(); String value = map.get(key); System.out.println(key + "\t" + value); String nextValue = map.get(key); if (value.equals(nextValue)) { map.remove(key); } } System.out.println(map); }
制作一个反向的HashMap!
HashMap map = new HashMap(); Set keys = map.keySet(); // The set of keys in the map. Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { String key = keyIter.next(); String value = map.get(key); map.add(value, key); }
既然你有hashMap,你需要反转它或打印它。
反正在迭代hashMap时不要删除。 将值保存在列表中并在外部循环中删除它们
Map mapValues = new HashMap(5); mapValues.put("1", "TJ"); mapValues.put("2", "Arun"); mapValues.put("3", "TJ"); mapValues.put("4", "Venkat"); mapValues.put("5", "Arun"); Collection
发生ConcurrentModificationException
,因为您要从map
中删除
if (value.equals(nextValue)) { map.remove(key); }
你必须从iterator
删除
if (value.equals(nextValue)) { keyIter.remove(key); }
来到重复条目问题,它非常简单: 在Java Map中查找重复值?
假设您使用Java 8 ,可以使用带有Set
的Stream API
来完成,它将存储现有值:
Map map = new HashMap<>(); map.put("A", "1"); ... System.out.printf("Before: %s%n", map); // Set in which we keep the existing values Set existing = new HashSet<>(); map = map.entrySet() .stream() .filter(entry -> existing.add(entry.getValue())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); System.out.printf("After: %s%n", map);
输出:
Before: {A=1, B=2, C=2, D=3, E=3} After: {A=1, B=2, D=3}
注意:严格地说,filter的谓词不应该是有状态的 ,它应该是javadoc中提到的无 状态 ,以确保即使我们使用并行流,结果仍然是确定的和正确的 。 但是在这里,我假设您不打算使用并行流,这样该方法仍然有效。
如果这是你的频繁要求,那么apache的commons.collections的 DualHashBidiMap
calss将帮助你更多而不是使用HashMap
。
这可以通过将hashmap放入arraylist来轻松完成。 这个arraylist是hashmap类型。
ArrayList> mArrayList=new ArrayList<>(); HashMap map=new HashMap<>(); map.put("1", "1"); mArrayList.add(map); map=new HashMap<>(); map.put("1", "1"); mArrayList.add(map); map=new HashMap<>(); map.put("1", "2"); mArrayList.add(map); map=new HashMap<>(); map.put("1", "3"); mArrayList.add(map); map=new HashMap<>(); map.put("1", "2"); mArrayList.add(map); for(int i=0;i
现在打印你的arraylist ...很容易删除hashmap中的所有重复值...这是删除重复的最简单方法
这将有助于从地图中删除重复值。
Map myMap = new TreeMap(); myMap.put("1", "One"); myMap.put("2", "Two"); myMap.put("3", "One"); myMap.put("4", "Three"); myMap.put("5", "Two"); myMap.put("6", "Three"); Set mySet = new HashSet (); for (Iterator itr = myMap.entrySet().iterator(); itr.hasNext();) { Map.Entry entrySet = (Map.Entry) itr.next(); String value = entrySet.getValue(); if (!mySet.add(value)) { itr.remove(); } }
System.out.println(“mymap:”+ mymap);
输出:
mymap:{1 =一,二=二,四=三}
public static void main(String[] args) { Map map = new HashMap<>(); map.put("A", "1"); map.put("B", "2"); map.put("C", "2"); map.put("D", "3"); map.put("E", "3"); System.out.println("Initial Map : " + map); for (String s : new ConcurrentHashMap<>(map).keySet()) { String value = map.get(s); for (Map.Entry ss : new ConcurrentHashMap<>(map) .entrySet()) { if (s != ss.getKey() && value == ss.getValue()) { map.remove(ss.getKey()); } } } System.out.println("Final Map : " + map); }
这可以使用Java 8完成。流的概念是必需的。 伪代码是stream()。filter()。collect()。 如果初始地图:{A = 1,B = 2,C = 2,D = 3,E = 3}。 然后删除重复项后所需的答案是{A = 1,B = 2,D = 3}。
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; public class RemoveDuplicates1 { public static void main(String[] args) { //Initial Map : {A=1, B=2, C=2, D=3, E=3} //After => {A=1, B=2, D=3} Map map = new HashMap<>(); map.put("A", "1"); map.put("B", "2"); map.put("C", "2"); map.put("D", "3"); map.put("E", "3"); System.out.printf("before : " +map ); System.out.println("\n"); Set set = new HashSet<>(); map = map.entrySet().stream() .filter(entry -> set.add(entry.getValue())) .collect(Collectors.toMap(Map.Entry :: getKey , Map.Entry :: getValue)); System.out.printf("after => " + map); } }