-2
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MyClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        hm.put(1, "Anil");
        hm.put(2, "Deven");
        hm.put(3, "sanjay");
        hm.put(4, "sanjay");
        hm.put(5, "Raj");
        hm.put(6, "sanjay");

        Set<Integer> keys = hm.keySet();

    }

}

This my code i want to remove all duplicate value from hash-map and want print on console please tell me how i will do this .

user3283502
  • 11
  • 1
  • 1
  • 4
  • 5
    How do you decide which key-pair needs to be kept and which needs to be removed as a duplicate? – Rahul Feb 07 '14 at 10:52
  • see i Just go Through Goodle in Arraylist i am able to remove duplicate but i just want to learn how i will remove from hasmap ? – user3283502 Feb 07 '14 at 10:54
  • You can find a solution here: [Find duplicate values in Java Map?][1] [1]: http://stackoverflow.com/questions/6895192/find-duplicate-values-in-java-map – Sandeep Feb 07 '14 at 11:21

3 Answers3

2

Your HashMap is hm. Put the values of hm in another HashMap hm2 in which the values of hm are the keys of hm2, and the values of hm2 can be anything (e.g. the object Boolean.TRUE).

Then loop through that second HashMap hm2 and print out its keys.

Instead of HashMap you can also use HashSet for hm2 (this is even better as you would not need the Boolean.TRUE part).

import java.util.HashMap;
import java.util.HashSet;

public class MyClass {

    public static void main(String[] args) {

        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        hm.put(1, "Anil");
        hm.put(2, "Deven");
        hm.put(3, "sanjay");
        hm.put(4, "sanjay");
        hm.put(5, "Raj");
        hm.put(6, "sanjay");

        HashSet<String> hm2 = new HashSet<String>();
        hm2.addAll(hm.values());

        for (String str : hm2){
            System.out.println(str);
        }
    }

}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
2

It is not possible to keep the mapping along with the key if you want duplicate values to be eliminated, as unique keys associated with duplicate values will also be dropped off from the map.

If you just need a list of unique values from the map try this:

Try this:

    Set<String> set = new HashSet<String>(hm.values());
    System.out.println(set);

Output: [Anil, Deven, Raj, sanjay]

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
2

Invert the map twice

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class UniqueMapValues
{
    public static void main(String[] args)
    {
        Map<Integer, String> hm = new HashMap<Integer, String>();
        hm.put(1, "Anil");
        hm.put(2, "Deven");
        hm.put(3, "sanjay");
        hm.put(4, "sanjay");
        hm.put(5, "Raj");
        hm.put(6, "sanjay");

        hm = invert(invert(hm));

        System.out.println(hm);
    }

    private static <K, V> Map<V, K> invert(Map<K, V> map)
    {
        Map<V, K> result = new HashMap<V, K>();
        for (Entry<K, V> entry : map.entrySet())
        {
            result.put(entry.getValue(), entry.getKey());
        }
        return result;
    }

}
Marco13
  • 53,703
  • 9
  • 80
  • 159