-1

I have to take union of sets (if there is n number then sets will be > n*(n-1)/2)

What I have done :
I have taken list of maps and then put the values in map

Given: n=size of list, l=length of map

List<Map<Integer, String>> maps = new ArrayList<Map<Integer, String>>(); 
      for(int i=0;i<n;i++){
          Map<Integer,String> myMap1 = new HashMap<Integer, String>(); 
           int l=in.nextInt();
           for(int j=0;j<l;j++){
               int num=in.nextInt();
               myMap1.put(num, "Val0");
           }
           //System.out.println(myMap1);
           maps.add(i,myMap1);
           //System.out.println(maps.get(i));
      }

What I Want:
To add every map with other so that I can find union.

I am getting the opposite result of Storing and Retrieving ArrayList values from hashmap

3
  • Do you want a single Map that contains the entries of all the original Maps? How would you handle duplicate keys (i.e. keys that appear in more than one Map, possibly with different values)? Commented Jun 5, 2017 at 10:14
  • 1
    @Eran i am storing in key values . bcz i dont want duplicacy Commented Jun 5, 2017 at 10:15
  • 1
    @Eran i want list of maps to store . and i want to add all these maps to find union. but i am success in storing maps in list view but how to retrieve these maps and add them ? Commented Jun 5, 2017 at 10:18

1 Answer 1

0

Are you looking for something like this:

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MapUnion {

    /**
     * This method adds all entries in all maps and return it as single map. It
     * takes Collection of elements of type Map<K, V>
     * 
     * @param maps - the Collection of maps, to sum all the entries
     * @return map containig "union" of all entries from all of the supplied maps
     */
    public <K, V> Map<K, V> unionAllMaps(Collection<Map<K, V>> maps) {
        return unionAllMaps(maps.toArray(new Map[maps.size()]));
    }

    /**
     * This method adds all entries in all maps and return it as single map. It
     * takes any numner of elements of type Map<K, V>. You can invoke it using
     * eg unionAllMaps(map1, map2, map3); the ... denotes, that all parameters
     * will be automatically converted to an array
     * 
     * @param maps - the Array of maps, to sum all the entries
     * @return map containig "union" of all entries from all of the supplied maps
     */
    public <K, V> Map<K, V> unionAllMaps(Map<K, V>... maps) {
        HashMap<K, V> union = new HashMap<K, V>();
        for (Map<K, V> map : maps) {
            union.putAll(map);
        }
        return union;
    }

    public static void main(String[] args) {
        new MapUnion().test();
    }

    public void test() {
        HashMap<Integer, String> map1 = new HashMap<Integer, String>();
        map1.put(1, "1");
        map1.put(2, "2");
        HashMap<Integer, String> map2 = new HashMap<Integer, String>();
        map1.put(2, "2");
        map1.put(3, "3");

        ArrayList<Map<Integer, String>> maps = new ArrayList<Map<Integer, String>>();
        maps.add(map1);
        maps.add(map2);

        Map<Integer, String> union = unionAllMaps(maps);

        System.out.println(union);
    }
}

will print:

{1=1, 2=2, 3=3}

But what should happen if there are same keys with different values in these maps? In this approach, the last value will overwrite previous one, but is this correct?

Sign up to request clarification or add additional context in comments.

6 Comments

Krzysztof Cichocki , i have n number of maps .mean they are variable as you have taken only two maps. help me if there n maps.
this will take any number of maps
No, i am talking about set of maps and their union
Ex: 3 maps - map1,map2,map3 then union(map1,map2) ,union(map2,map3),union(map3,map1),union(map1,map2,map3)
yes, add them all to the ArrayList maps and it will make a union from all entries in all maps.
|

Your Answer

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