9

I want to display the values in a HashMap. A HashMap may have duplicate values (but not duplicate keys), but I want to display a value only once.

So I should find whether the Map has duplicate values. I know we can iterate over the Map and use the return boolean of map.containsValue(value). I want to know whether any method exists to find duplicate values in map or we should I write code myself?

Nivas
  • 17,720
  • 4
  • 58
  • 75
Silambarasan
  • 311
  • 2
  • 4
  • 13

7 Answers7

24

A simple solution would be to compare the size of your values list with your values set.

// pseudo-code
List<T> valuesList = map.values();
Set<T> valuesSet = new HashSet<T>(map.values);
// check size of both collections; if unequal, you have duplicates
Sanjay T. Sharma
  • 22,282
  • 4
  • 58
  • 71
  • This tech is fine. but if I want to remove duplicate mean should do manual remove operation right? – Silambarasan Aug 01 '11 at 09:09
  • Yes, you'll have to do a manual operation. But if you can explain me the *exact* scenario, as in how you end up with multiple keys having same value and why you want to remove them, maybe I can propose a better solution. – Sanjay T. Sharma Aug 01 '11 at 10:42
6

Example:

Map<Object, Object> map = new HashMap<Object, Object>();
map.put(1,2);
map.put(3,4);
map.put(2,2);
map.put(5,3);

Set<Object> uniqueValues = new HashSet<Object>(map.values());

System.out.println(uniqueValues);

Output:

[2, 3, 4]
Adriaan Koster
  • 15,325
  • 4
  • 44
  • 60
2

Try out this code

private boolean hasDuplicates(Map<Integer, List<String>> datamap){
boolean status = false;


    Set valueset=new HashSet(datamap.values());

    if(datamap.values().size()!=valueset.size()){
    status=true;
    }
    else{
    status = false;
    }


    return status;

}
achini
  • 199
  • 1
  • 6
1

Use apache commons library class's method

org.apache.commons.collections.MapUtils.invertMap(map)

and compare the size of actual map and invert map.

NIrav Modi
  • 4,676
  • 6
  • 27
  • 38
1
public static void main(String[] args) {

        HashMap<String, Integer> map = new HashMap<>();
        map.put("abc", 2);
        map.put("def", 1);
        map.put("hij", 4);
        map.put("klm", 6);
        map.put("nop", 2);
        map.put("qrs", 2);
        map.put("tuv", 6);
        map.put("wxy", 8);
        map.put("zab", 1);
        map.put("cde", 5);
        map.put("fgh", 4);
        map.put("ijk", 3);

        HashMap<Integer, String> duplicatMap = new HashMap<>();

        Set<Entry<String, Integer>> entrySet = map.entrySet();
        Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
        while(iterator.hasNext()) {
            Entry<String, Integer> entry = iterator.next();
            String key = entry.getKey();
            Integer value = entry.getValue();

            if(duplicatMap.containsKey(value)) {
                duplicatMap.put(value, duplicatMap.get(value)+", "+key);
            } else {
                duplicatMap.put(value, key);
            }
        }
        System.out.println(duplicatMap);

    } 

outPut: - {1=def, zab, 2=abc, qrs, nop, 3=ijk, 4=fgh, hij, 5=cde, 6=tuv, klm, 8=wxy} if you want to modify then use again EntrySet.

Mani Kumar
  • 31
  • 3
1

There is no such method provided as of jdk1.6.

One simple way you can do is

  • get all the values from the map in a list
  • put that list into a set which will remove the duplicates
Lucky
  • 15,935
  • 19
  • 113
  • 150
Swagatika
  • 3,306
  • 6
  • 29
  • 38
0
try this code but this is not optimize code :

public class HashMapDulicate {
    public static void main(String[] args) {        
        Map<String,Integer> map=new HashMap<>();
        map.put("A", 1);
        map.put("B", 1);
        map.put("C", 3);
        map.put("D", 4);


        Set set=new HashSet<>();
        List list=new ArrayList<>();

        for(Entry<String, Integer> mapVal:map.entrySet()) {

            if(!set.add(mapVal.getValue())) {
                list.add(mapVal.getValue());

            }else {
                set.add(mapVal.getValue());
            }

        }

for(Entry<String, Integer> mapVal:map.entrySet()) {

    if(list.contains(mapVal.getValue())){

        System.out.println(mapVal.getKey() +":" + mapVal.getValue());
    }
}
    }
}
Pandey Praveen
  • 95
  • 2
  • 11