@Test public void groupWithDuplicateKeysAndValues() { Map<Integer, Set<Integer>> expected = new HashMap<>(); expected.put(0, new HashSet<>(Collections.singletonList(1))); expected.put(1, new HashSet<>(Arrays.asList(2, 3))); final Map<Integer, Set<Integer>> result = GROUPING_HELPER.groupWithDuplicateValues(number -> number / 2); Assert.assertEquals("Unexpected map content", expected, result); }
public <KeyT> Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT, Collection<KeyT>> keysExtractor) { Map<KeyT, Set<ValueT>> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final Collection<KeyT> keys = keysExtractor.apply(value); for (KeyT key : keys) { Set<ValueT> valuesForKey = groupedEntities.computeIfAbsent(key, k -> new HashSet<>()); valuesForKey.add(value); } } return groupedEntities; }
GroupingHelper { public <KeyT> Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT, Collection<KeyT>> keysExtractor) { Map<KeyT, Set<ValueT>> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final Collection<KeyT> keys = keysExtractor.apply(value); for (KeyT key : keys) { Set<ValueT> valuesForKey = groupedEntities.computeIfAbsent(key, k -> new HashSet<>()); valuesForKey.add(value); } } return groupedEntities; } }
GroupingHelper { public <KeyT> Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT, Collection<KeyT>> keysExtractor) { Map<KeyT, Set<ValueT>> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final Collection<KeyT> keys = keysExtractor.apply(value); for (KeyT key : keys) { Set<ValueT> valuesForKey = groupedEntities.computeIfAbsent(key, k -> new HashSet<>()); valuesForKey.add(value); } } return groupedEntities; } GroupingHelper(Collection<ValueT> collection); }
GroupingHelper { public <KeyT> Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT, Collection<KeyT>> keysExtractor) { Map<KeyT, Set<ValueT>> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final Collection<KeyT> keys = keysExtractor.apply(value); for (KeyT key : keys) { Set<ValueT> valuesForKey = groupedEntities.computeIfAbsent(key, k -> new HashSet<>()); valuesForKey.add(value); } } return groupedEntities; } GroupingHelper(Collection<ValueT> collection); Map<KeyT, Set<ValueT>> groupWithDuplicateValues(
            Function<ValueT, KeyT> keyExtractor); Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT,
            Collection<KeyT>> keysExtractor); Map<KeyT, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor); }
GroupingHelper { public <KeyT> Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT, Collection<KeyT>> keysExtractor) { Map<KeyT, Set<ValueT>> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final Collection<KeyT> keys = keysExtractor.apply(value); for (KeyT key : keys) { Set<ValueT> valuesForKey = groupedEntities.computeIfAbsent(key, k -> new HashSet<>()); valuesForKey.add(value); } } return groupedEntities; } GroupingHelper(Collection<ValueT> collection); Map<KeyT, Set<ValueT>> groupWithDuplicateValues(
            Function<ValueT, KeyT> keyExtractor); Map<KeyT, Set<ValueT>> groupWithDuplicateKeysAndValues(Function<ValueT,
            Collection<KeyT>> keysExtractor); Map<KeyT, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor);  }