CollectionUtil { public static <K, V> Map<V, List<K>> invertListMap(Map<K, List<V>> key2ValuesMap) { One2ManyListMap<V, K> value2KeysMap = new One2ManyListMap<V, K>(); for (K shardName : key2ValuesMap.keySet()) { List<V> nodes = key2ValuesMap.get(shardName); for (V node : nodes) { value2KeysMap.add(node, shardName); } } return value2KeysMap.asMap(); }  static Map<V, List<K>> invertListMap(Map<K, List<V>> key2ValuesMap); static List<String> getListOfAdded(final Collection<String> oldList, final Collection<String> updatedList); static Set<String> getSetOfAdded(final Collection<String> oldSet, final Collection<String> updatedSet); static List<String> getListOfRemoved(final Collection<String> oldList, final Collection<String> updatedList); static Set<String> getSetOfRemoved(final Collection<String> oldSet, final Collection<String> updatedSet);  }
@Test public void testInvertListMap() throws Exception { Map<String, List<Integer>> map = new HashMap<String, List<Integer>>(); map.put("a", Arrays.asList(1, 2, 3)); map.put("b", Arrays.asList(2, 4)); Map<Integer, List<String>> invertMap = CollectionUtil.invertListMap(map); assertEquals(4, invertMap.size()); assertTrue(invertMap.containsKey(1)); assertTrue(invertMap.containsKey(2)); assertTrue(invertMap.containsKey(3)); assertTrue(invertMap.containsKey(4)); assertTrue(invertMap.get(1).contains("a")); assertTrue(invertMap.get(2).contains("a")); assertTrue(invertMap.get(3).contains("a")); assertTrue(invertMap.get(2).contains("b")); assertTrue(invertMap.get(4).contains("b")); int valueCount = 0; Collection<List<String>> values = invertMap.values(); for (List<String> valueList : values) { valueCount += valueList.size(); } assertEquals(5, valueCount); }
@Test public void testInvertListMap_EmptyValues() throws Exception { Map<String, List<Integer>> map = new HashMap<String, List<Integer>>(); map.put("a", Collections.EMPTY_LIST); map.put("b", Arrays.asList(2, 4)); Map<Integer, List<String>> invertMap = CollectionUtil.invertListMap(map); assertEquals(2, invertMap.size()); assertTrue(invertMap.get(2).contains("b")); assertTrue(invertMap.get(4).contains("b")); }