从Java中删除HashMap中的重复值

use*_*852 6 java hashmap duplicates

我有一个重复值的地图:

("A", "1");
("B", "2");
("C", "2");
("D", "3");
("E", "3");
Run Code Online (Sandbox Code Playgroud)

我想要地图了

("A", "1");
("B", "2");
("D", "3");
Run Code Online (Sandbox Code Playgroud)

你知道如何摆脱重复的价值吗?

目前,我收到'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);
}
Run Code Online (Sandbox Code Playgroud)

No *_*ame 8

制作一个反向的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);
}
Run Code Online (Sandbox Code Playgroud)

既然你有hashMap,你需要反转它或打印它.

反正在迭代hashMap时不要删除.将值保存在列表中并在外部循环中删除它们

  • 保持哪个元素仍然是随机的(因为`HashMap`的顺序是未定义的),但如果没有问题,这样可以正常工作. (3认同)
  • @Heuster 我同意,但他没有说这是一个问题 (2认同)

Nic*_*tto 7

假设您使用Java 8,可以使用存储现有值的Stream APIwith来完成Set<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);     
Run Code Online (Sandbox Code Playgroud)

输出:

Before: {A=1, B=2, C=2, D=3, E=3}
After: {A=1, B=2, D=3}
Run Code Online (Sandbox Code Playgroud)

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


小智 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();
        }
    }
Run Code Online (Sandbox Code Playgroud)


Sur*_*tta 2

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

  if (value.equals(nextValue)) {
            map.remove(key);
        }
Run Code Online (Sandbox Code Playgroud)

你必须从iterator

if (value.equals(nextValue)) {
            keyIter.remove(key);
        }
Run Code Online (Sandbox Code Playgroud)

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

  • 好吧,如果你看清楚他的代码,这并不能真正解决他的问题。 (3认同)