1

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.

1
  • A Map cannot 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 Commented Jan 14, 2016 at 3:09

2 Answers 2

5

This:

Map<String , Object> map = new HashMap<String , Object>();
Set<String> valueSet = new HashSet<String>(map.values());

Doesn't work because map.values() returns a Collection<Object> and the set is of type String.

Try this:

Map<String , Object> map = new HashMap<String , Object>();
Set<Object> valueSet = new HashSet<Object>(map.values());    

Or:

Map<String , String> map = new HashMap<String , String>();
Set<String> valueSet = new HashSet<String>(map.values());  

@coders in response to your comment, if you have a list of maps and you want to create a set of this maps's unique values, you can do this:

ArrayList<HashMap<String, Object>> mDataList = new ArrayList<HashMap<String, Object>>();
Set<Object> valueSet = new HashSet<Object>();  
for(HashMap<String, Object> map : mDataList){
    valuesSet.addAll(map.values());
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hello @Titus, how to loop through the list of hashmap<string, object> to eliminate the duplicates. Ex: ArrayList<HashMap<String, Object>> mDataList = new ArrayList<HashMap<String, Object>>();
@coders what do you mean by duplicates ? maps with the same entries or multiple maps having the same key or the same value ?
Thanks, But how can you add this(<HashMap<String, Object> map : mDataList) inside the for loop. Thats not correct right?
@coders that was a typo, it had an < at the start that shouldn't have been there.
2

Hasmap cannot store duplicate keys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.