I'm having haspmap of type <String, Object>. This hashmap contains the duplicates entries. I'm trying the below method to remove the duplicates. But its showing an error. Please find the code and error as below.
This is the original hashmap which contains data.
Map<String , Object> map = new HashMap<String , Object>();
I'm trying to add the values to sets.
Set<String> valueSet = new HashSet<String>(map.values());
In the map.values() its showing an error saying that cannot resolve the constructor hashset(java.util.collection)
After this I'm adding the below code to eliminate the duplicates.
Iterator<String> it = valueSet.iterator();
Map<Integer , String> uniqueMap = new HashMap<Integer , String>();
while(it.hasNext()){
String value = it.next();
for(Entry<Integer , String> e : map.entrySet()){
if(value.equals(e.getValue()) && !uniqueMap.containsValue(value)){
uniqueMap.put(e.getKey(), value);
}
}
}
But due the error, I'm not able to proceed further. Could you please help me where I'm doing wrong. Thank you.
Mapcannot have duplicate keys, but maybe duplicate values. If what you want/need is to have both unique keys and values/ you can use a BiMap from Google Guava's library or use this algorithm: 1) create a new map, 2) for each key/value pair in your original map, store them in the new map using the value as key and the key as value 3) store the values from the new map back in the old map