Remove Duplicate Values from Hashmap in Java

How to remove duplicates from Hashmap of type HashMap<String, Object>?

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());
}

How to remove duplicate values from a HashMap

You can do it with the following method which only iterates over the map once:

private static void removeTheFirstNameDuplicates(final Map<String, String> map) {
final Iterator<Entry<String, String>> iter = map.entrySet().iterator();
final HashSet<String> valueSet = new HashSet<String>();
while (iter.hasNext()) {
final Entry<String, String> next = iter.next();
if (!valueSet.add(next.getValue())) {
iter.remove();
}
}
}

The add() method on HashSet will return false if a value has already been added to the set. The method above uses this to detect that a duplicate has been found and then removes the duplicate from the HashMap by using the remove() method on the iterator.

It is worth noting that, depending on the Map implementation you use, the iteration order may not be guaranteed so which duplicate you remove is also not guaranteed.

If you were to use a TreeMap rather than a HashMap you would be certain to iterate over the map alphabetically by key e.g. Berluccio, Bracco, Carleone … Verdo. You would then always keep Stradivari and remove Vivaldi.

how to remove duplicate value from hash map

Your HashMap is hm. Put the values of hm in another HashMap hm2 in which the values of hm are the keys of hm2, and the values of hm2 can be anything (e.g. the object Boolean.TRUE).

Then loop through that second HashMap hm2 and print out its keys.

Instead of HashMap you can also use HashSet for hm2 (this is even better as you would not need the Boolean.TRUE part).

import java.util.HashMap;
import java.util.HashSet;

public class MyClass {

public static void main(String[] args) {

HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1, "Anil");
hm.put(2, "Deven");
hm.put(3, "sanjay");
hm.put(4, "sanjay");
hm.put(5, "Raj");
hm.put(6, "sanjay");

HashSet<String> hm2 = new HashSet<String>();
hm2.addAll(hm.values());

for (String str : hm2){
System.out.println(str);
}
}

}

How do I remove duplicates in a Linked HashMap?

The default behavior will Override the existing value with the existing key, So in the LinkedHashMap has the following values

{The=0, Hello=21, Katherine=17}

, And you can check if the key is already exists using containsKey()

if (wordIndex.containsKey("Hello")) {
// do something
}

How to remove duplicate list from hashmap which is added as values

May be this piece of code will work for you -

public static void main(String[] args) {
Map<String, List<Integer>> sample = new HashMap<String, List<Integer>>();
List first = new ArrayList();
first.add(1);
first.add(2);
first.add(3);
List second = new ArrayList();
second.add(4);
second.add(5);
second.add(6);
List third = new ArrayList();
third.add(1);
third.add(2);
third.add(3);
sample.put("first", first);
sample.put("second", second);
sample.put("third", third);
removeDuplicates(sample);
System.out.print(sample.size()); // now it will print 2

}

private static void removeDuplicates(Map<String, List<Integer>> sample) {
Collection<List<Integer>> list = sample.values();
for(Iterator<List<Integer>> itr = list.iterator(); itr.hasNext();) {
if(Collections.frequency(list, itr.next())>1) {
itr.remove();
}
}
}

This remove duplicate method will remove the duplicate values from the List.

How to remove duplicate key from a Hashmap

Try something like this:

List<List<Integer>> result = Arrays.asList(
Arrays.asList(-1, 0, 1),
Arrays.asList(-1, 2, -1),
Arrays.asList(0, 1, -1)
);
HashMap<Set<Integer>, List<Integer>> final_sol = new HashMap<>();

for (List<Integer> list : result) {
final_sol.put(new HashSet<>(list), list);
}

System.out.println(final_sol.values());

You think, that [-1,0,1] and [0,1,-1] they are same, but this does not hold for lists, because order of elements matters. You need sets for this understanding of equals.

But even then it may not be what you expected, when [0,0,1] and [0,1,1] are same. In this case you must transform each list to a Map which gives you the count of the same Integer in the original list.

how to remove duplicate results generating from hashmap

I would just use a Set to keep track of which complements you've already seen:

Set<Integer> seen = new HashSet<>();        
for (Integer in : m.keySet())
{
if(m.containsKey(sum-in) && !seen.contains(in))
{
System.out.println(m.get(in) + "," + m.get(sum-in));
seen.add(sum-in);
}
}

You can do it without any additional storage if you're able to modify the map, at the loss of some clarity:

for (Integer in : m.keySet())
{
if(m.containsKey(sum-in) && m.get(in) != null)
{
System.out.println(m.get(in) + "," + m.get(sum-in));
m.put(sum-in, null);
}
}

Output:

a,e
d,b
c,f


Related Topics



Leave a reply



Submit