9

我有一个重复值的地图:

("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<String, String> map = new HashMap<String, String>();
    map.put("A", "1");
    map.put("B", "2");
    map.put("C", "2");
    map.put("D", "3");
    map.put("E", "3");

    Set<String> keys = map.keySet(); // The set of keys in the map.

    Iterator<String> 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);
}
4

10 回答 10

9

做一个反向HashMap!

HashMap<String, String> map = new HashMap<String, String>();
Set<String> keys = map.keySet(); // The set of keys in the map.

Iterator<String> keyIter = keys.iterator();

while (keyIter.hasNext()) {
    String key = keyIter.next();
    String value = map.get(key);
    map.put(value, key);
}

现在您有了 hashMap,您需要反转它或打印它。

无论如何不要在迭代 hashMap 时删除。将值保存在列表中并在外部循环中删除它们

于 2013-07-23T14:06:29.040 回答
7

假设您使用Java 8,可以使用Stream APIwith aSet<String>来存储现有值:

Map<String, String> map = new HashMap<>();
map.put("A", "1");
...
System.out.printf("Before: %s%n", map);

// Set in which we keep the existing values
Set<String> 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}

注意:严格来说,过滤器的谓词不应该是有状态的,它应该是javadoc中提到的无状态的,以确保即使我们使用并行流,结果也保持确定性正确性。但是在这里,我假设您不打算使用并行流,因此这种方法仍然有效。

于 2016-10-19T14:53:11.047 回答
5
    Map<String,Object> mapValues = new HashMap<String,Object>(5);
    mapValues.put("1", "TJ");
    mapValues.put("2", "Arun");
    mapValues.put("3", "TJ");
    mapValues.put("4", "Venkat");
    mapValues.put("5", "Arun");

    Collection<Object> list = mapValues.values();
    for(Iterator<Object> itr = list.iterator(); itr.hasNext();)
    {
        if(Collections.frequency(list, itr.next())>1)
        {
            itr.remove();
        }
    }
于 2014-10-01T06:41:37.193 回答
2

ConcurrentModificationException正在发生,因为你正在从map

  if (value.equals(nextValue)) {
            map.remove(key);
        }

你必须从iterator

if (value.equals(nextValue)) {
            keyIter.remove(key);
        }

来到重复条目问题,它非常简单:在 Java Map 中查找重复值?

于 2013-07-23T14:03:33.437 回答
2

这可以使用 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<String , String > 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<String> 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);

   }
}
于 2018-03-04T07:38:39.113 回答
1

如果这是您经常需要的,那么DualHashBidiMapapache 的commons.collections的类会帮助您更多,而不是使用HashMap.

于 2013-07-23T14:47:13.950 回答
1
public static void main(String[] args) {
    Map<String, String> 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<String, String> ss : new ConcurrentHashMap<>(map)
                .entrySet()) {
            if (s != ss.getKey() && value == ss.getValue()) {
                map.remove(ss.getKey());
            }
        }
    }
    System.out.println("Final Map : " + map);
}
于 2014-08-06T13:26:43.413 回答
0

这将有助于从地图中删除重复值。

    Map<String, String> myMap = new TreeMap<String, String>();
    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<String> mySet = new HashSet<String>();

    for (Iterator itr = myMap.entrySet().iterator(); itr.hasNext();)
    {
        Map.Entry<String, String> entrySet = (Map.Entry) itr.next();

        String value = entrySet.getValue();

        if (!mySet.add(value))
        {
            itr.remove();               
        }
    }

System.out.println("我的地图:" + mymap);

输出:

我的地图:{1=一,2=二,4=三}

于 2014-04-03T03:04:39.210 回答
0

这可以通过将您的 hashmap 放入 arraylist 来轻松完成。这个arraylist 是hashmap 类型的。

ArrayList<HashMap<String, String>> mArrayList=new ArrayList<>();
HashMap<String, String> 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<mArrayList.size();i++)
        {
            temp=mArrayList.get(i).get("1");
            for(int k=i+1;k<mArrayList.size();k++)
            {
                if(temp.equals(mArrayList.get(k).get("1")))
                {
                    mArrayList.remove(k); 
                } 
            }

        }

现在打印您的数组列表...从哈希图中轻松删除所有重复值...这是删除重复的最简单方法

于 2014-02-06T05:28:45.303 回答
0

如果您只是想删除 concurrentModification 异常,那么只需将您的 HashMap 替换为 ConcurrentHashMap。

要了解更多关于 ConcurrentHashMap 看这里

于 2019-01-23T13:41:53.203 回答