@Test public void groupUnique() { Map<Integer, Integer> expected = new HashMap<>(); expected.put(2, 1); expected.put(4, 2); expected.put(6, 3); final Map<Integer, Integer> result = GROUPING_HELPER.groupUnique(number -> number * 2); Assert.assertEquals("Unexpected map content", expected, result); }
public <KeyT> Map<KeyT, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor) { Map<KeyT, ValueT> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final KeyT key = keyExtractor.apply(value); if (groupedEntities.get(key) != null) { throw new DuplicateElementException(key, collection); } groupedEntities.put(key, value); } return groupedEntities; }
GroupingHelper { public <KeyT> Map<KeyT, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor) { Map<KeyT, ValueT> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final KeyT key = keyExtractor.apply(value); if (groupedEntities.get(key) != null) { throw new DuplicateElementException(key, collection); } groupedEntities.put(key, value); } return groupedEntities; } }
GroupingHelper { public <KeyT> Map<KeyT, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor) { Map<KeyT, ValueT> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final KeyT key = keyExtractor.apply(value); if (groupedEntities.get(key) != null) { throw new DuplicateElementException(key, collection); } groupedEntities.put(key, value); } return groupedEntities; } GroupingHelper(Collection<ValueT> collection); }
GroupingHelper { public <KeyT> Map<KeyT, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor) { Map<KeyT, ValueT> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final KeyT key = keyExtractor.apply(value); if (groupedEntities.get(key) != null) { throw new DuplicateElementException(key, collection); } groupedEntities.put(key, 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, ValueT> groupUnique(Function<ValueT, KeyT> keyExtractor) { Map<KeyT, ValueT> groupedEntities = new HashMap<>(); for (ValueT value : collection) { final KeyT key = keyExtractor.apply(value); if (groupedEntities.get(key) != null) { throw new DuplicateElementException(key, collection); } groupedEntities.put(key, 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);  }