Remove duplicate values from HashMap in Java

前端 未结 10 939
南旧
南旧 2020-12-09 21:02

I have a map with duplicate values:

(\"A\", \"1\");
(\"B\", \"2\");
(\"C\", \"2\");
(\"D\", \"3\");
(\"E\", \"3\");

I would like to the map

相关标签:
10条回答
  • 2020-12-09 21:50

    This can be easily done by putting your hashmap into arraylist. This arraylist is of hashmap type.

    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); 
                    } 
                }
    
            }
    

    Now print your arraylist...all the duplicate values from the hashmap easily removed...This is the easiest way to remove duplicacy

    0 讨论(0)
  • 2020-12-09 21:56
    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);
    }
    
    0 讨论(0)
  • 2020-12-09 21:57

    This will be helpful to remove duplicate values from map.

        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 :" + mymap);

    Output:

    mymap :{1=One, 2=Two, 4=Three}

    0 讨论(0)
  • 2020-12-09 22:00

    ConcurrentModificationException happening,because you are removing from map

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

    You have to remove from iterator

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

    Coming to the duplicate entry issue,Its pretty simple :Find duplicate values in Java Map?

    0 讨论(0)
提交回复
热议问题