Find duplicate values in Java Map?
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]
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;
}
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