Home > database >  Finding which keys and values to add/remove from hashmap1 to be equal to hashmap2
Finding which keys and values to add/remove from hashmap1 to be equal to hashmap2

Time:11-04

Supposing I have the following situation:

I want to find which keys and values to add/remove from hashmap1 in order hashmap1 and hashmap2 have the same values.

hashmap1: {obj2=[Brazil], obj1=[Argentina, Chile, Brazil], obj3Mobile=[Russia]}

hashmap2: {obj3Op=[Germany], obj2=[Brazil, China], obj1=[Argentina, Brazil]}

My expected output is:

add:  {obj2=[China], obj3Op=[Germany]}

remove: {obj3Mobile=[Russia], obj1=[Chile]}

In order to reproduce the dataset:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

public class Main
{
    public static void main(String[] args) {
        Map<String, List<String>> dictOP = new HashMap<String, List<String>>();
        Map<String, List<String>> dictMobile = new HashMap<String, List<String>>();
        
        List<String> obj1Mobile = new ArrayList<String>();
        List<String> obj2Mobile = new ArrayList<String>();
        List<String> obj3Mobile = new ArrayList<String>();
        
        List<String> obj1Op = new ArrayList<String>();
        List<String> obj2Op = new ArrayList<String>();
        List<String> obj3Op = new ArrayList<String>();
        
        
        obj1Mobile.add("Argentina");
        obj1Mobile.add("Chile");
        obj1Mobile.add("Brazil");
        obj2Mobile.add("Brazil");
        obj3Mobile.add("Russia");
        
        
        obj1Op.add("Argentina");
        obj1Op.add("Brazil");
        obj2Op.add("Brazil");
        obj2Op.add("China");
        obj3Op.add("Germany");
        
        dictOP.put("obj1", obj1Op);
        dictOP.put("obj2", obj2Op);
        dictOP.put("obj3Op", obj3Op);
        
        dictMobile.put("obj1", obj1Mobile);
        dictMobile.put("obj2", obj2Mobile);
        dictMobile.put("obj3Mobile", obj3Mobile);
        
        System.out.println(dictMobile);
        System.out.println(dictOP);
        

    }
}

Using this approach below, I could only find which keys to add and remove.

    //Union of keys from both maps
    HashSet<String> removeKey = new HashSet<>(dictMobile.keySet());
    removeKey.addAll(dictOP.keySet());
    removeKey.removeAll(dictMobile.keySet());
     
    HashSet<String> addKey = new HashSet<>(dictOP.keySet());
    addKey.addAll(dictMobile.keySet());
    addKey.removeAll(dictOP.keySet());
    
    System.out.println(removeKey);
    System.out.println(addKey);

But I could not found a simple way to get both keys and values together

CodePudding user response:

One way to do this is to first flatten the Map<String, List<String>> into lots of <string, string> pairs. Then operate on that. And finally combine the results back to a Map<String, List<String>>.

So first, you will need to write helper methods that do the flattening and combining:

private static Set<Entry<String, String>> flatten(Map<String, List<String>> map) {
    return map.entrySet().stream().flatMap(e ->
          e.getValue().stream().map(v -> Map.entry(e.getKey(), v))
        )
        .collect(Collectors.toSet());
}

private static Map<String, List<String>> group(Set<Map.Entry<String, String>> entries) {
    return entries.stream().collect(Collectors.groupingBy(
        Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())
    ));
}

Then it is very straightforward:

var mobileEntries = flatten(dictMobile);
var opEntries = flatten(dictOP);
var add = new HashSet<>(opEntries);
add.removeAll(mobileEntries);
var remove = new HashSet<>(mobileEntries);
remove.removeAll(opEntries);
System.out.println(group(add));
System.out.println(group(remove));

This prints:

{obj3Op=[Germany], obj2=[China]}
{obj1=[Chile], obj3Mobile=[Russia]}

CodePudding user response:

But I could not found a simple way to get both keys and values together

Yes, you can get keys and values together from a map using the entrySet() method.

And then you can use it like this

for (var entry : map.entrySet()) {
    System.out.println(entry.getKey()   "/"   entry.getValue());
}
  • Related