Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicate values in Java Map?

Tags:

java

hashmap

map

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?

like image 983
Silambarasan Avatar asked Aug 01 '11 07:08

Silambarasan


1 Answers

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;

}
like image 188
achini Avatar answered Oct 08 '22 09:10

achini