Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

public class Main {
    public static <K, V> Map<K, Set<V>> combineMapSets(Map<K, Set<V>> map1, Map<K, Set<V>> map2) {
        Map<K, Set<V>> combined = Maps.newHashMap();
        addMapSet(combined, map1);
        addMapSet(combined, map2);
        return combined;
    }

    public static <K, V> void addMapSet(Map<K, Set<V>> map, Map<K, Set<V>> toAdd) {
        for (Map.Entry<K, Set<V>> entry : toAdd.entrySet()) {
            Set<V> values = map.get(entry.getKey());
            if (values == null) {
                values = Sets.newHashSet();
                map.put(entry.getKey(), values);
            }
            for (V value : entry.getValue()) {
                values.add(value);
            }
        }
    }
}