且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 Java 中的 HashMap 中删除重复值

更新时间:2022-12-12 19:18:50

做一个反向HashMap!

make a reverse 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,您需要将其反转或打印出来.

now that you have the hashMap you need reverse it or print it.

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

in anyway do not delete while iterating hashMap. save the values in a list and delete them in an outer loop