2

I have two (say, map1 and map2) that may contain same set of keys but different values. For each matching keys in both I want to put the key and its corresponding values from both into a third Multimap. I've browsed through all related questions on StackOverFlow, non of them provides a concrete solution to the problem.

My question was marked duplicate of Combine two Maps into a MultiMap . My question seek to collect from a two different Guava HashMultimap (not java.util.Map) into a third Guava HashMultimap.

Multimap<String, Collection<ArrayList<Double>>> combineMaps(Multimap<String, ArrayList<Double>> map1, Multimap<String, ArrayList<Double>> map2) {

    Collection<ArrayList<Double>> combinedList = new ArrayList<ArrayList<Double>>(); 
    Collection<ArrayList<Double>> list1 = new ArrayList<ArrayList<Double>>();
    Collection<ArrayList<Double>> list2 = new ArrayList<ArrayList<Double>>();

    map1.forEach((key, value) -> {
        if(map2.containsKey(key)) {
            list1 = (map1.get(key));
            list2 = (map2.get(key));
            combinedList.addAll(list1);
            combinedList.addAll(list2);
            multimap.put(key, combinedList);
        }
    });
    return multimap;
}

For example, if map1 contains the key-values pairs key1 = {2.0, 3.1}

keyb = {3.2, 0.8}

And map2 contains keyz = {2.7, 1.2}

keyb = {5.9, 7.6}

key1 = {3.1, 9.4}

The combinedMap would contain key1 = {2.0, 3.1, 3.1, 9.4}

keyb = {3.2, 0.8, 5.9, 7.6}

Note that keyz won't be saved into combinedMap because there is no occurence of keyz in map1.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
Ercross
  • 519
  • 6
  • 17
  • Possible duplicate of [Combine two Maps into a MultiMap](https://stackoverflow.com/questions/9324154/combine-two-maps-into-a-multimap) – Bakon Jarser Apr 25 '19 at 18:23
  • Thanks! I was focusing on his [java] tag. It should be [guava] – WJS Apr 25 '19 at 18:35
  • 1
    @BakonJarser, the question is not a possible duplicate of the one you specificied. The other question has two java.util.Map has argument, but this had Guava Multimaps as argument. It's not to just merge/combine two multimaps, but to also save values of matching keys in both multimaps into an ArrayList container for each key in both map1, map2. – Ercross Apr 29 '19 at 07:42
  • @WJS [guava] should essentially always be accompanied by [java]. It's not a different language. – Louis Wasserman Apr 29 '19 at 15:59
  • So what problems are you having with the code you wrote? The only mistake I see is that you don't call combinedList.clear() on every loop iteration. – Bakon Jarser Apr 29 '19 at 16:21
  • @Bakon Jarser, the problem I was having with the code is that it's not the best tool to use for the purpose I intended. I needed a data structure to hold more than one table together, minimum of 4 tables and could increase in the future where I can return a value (of type specified by value) by giving its row key and a column key. Guava HashBasedTable solved the problem. All tables are then put into an array. I eventually got an Excel spreadsheet kinda structure. Thanks. – Ercross May 08 '19 at 20:15

0 Answers0