Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/apache/commons-collections
Browse files Browse the repository at this point in the history
…into feature/findDuplicates
  • Loading branch information
hemanth0525 committed Sep 2, 2024
2 parents dd78ec6 + 5db9d7c commit 053d2dd
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 191 deletions.
57 changes: 0 additions & 57 deletions src/main/java/org/apache/commons/collections4/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
Expand Down Expand Up @@ -762,62 +761,6 @@ public static <O> Collection<O> disjunction(final Iterable<? extends O> a, final
return helper.list();
}

/**
* Finds and returns the List of duplicate elements in the given collection.
*
* @param <E> the type of elements in the collection.
* @param collection the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
* @since 4.5.0-M3
*/
public static <E> List<E> duplicateList(final Collection<E> collection) {
return new ArrayList<>(duplicateSet(collection));
}

/**
* Finds and returns the sequenced Set of duplicate elements in the given collection.
* <p>
* Once we are on Java 21 and a new major version, the return type should be SequencedSet.
* </p>
*
* @param <E> the type of elements in the collection.
* @param collection the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
* @since 4.5.0-M3
*/
public static <E> Set<E> duplicateSequencedSet(final Collection<E> collection) {
return duplicateSet(collection, new LinkedHashSet<>());
}

/**
* Finds and returns the set of duplicate elements in the given collection.
*
* @param <E> the type of elements in the collection.
* @param collection the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
* @since 4.5.0-M3
*/
public static <E> Set<E> duplicateSet(final Collection<E> collection) {
return duplicateSet(collection, new HashSet<>());
}

/**
* Worker method for {@link #duplicateSet(Collection)} and friends.
*
* @param <C> the type of Collection.
* @param <E> the type of elements in the Collection.
* @param collection the list to test, must not be null.
* @param duplicates the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
*/
static <C extends Collection<E>, E> C duplicateSet(final Collection<E> collection, final C duplicates) {
final Set<E> set = new HashSet<>();
for (final E e : collection) {
(set.contains(e) ? duplicates : set).add(e);
}
return duplicates;
}

/**
* Returns the immutable EMPTY_COLLECTION with generic type safety.
*
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/org/apache/commons/collections4/IterableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -345,6 +347,62 @@ public static <E> long countMatches(final Iterable<E> input, final Predicate<? s
return size(filteredIterable(emptyIfNull(input), predicate));
}

/**
* Finds and returns the List of duplicate elements in the given collection.
*
* @param <E> the type of elements in the collection.
* @param iterable the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
* @since 4.5.0-M3
*/
public static <E> List<E> duplicateList(final Iterable<E> iterable) {
return new ArrayList<>(duplicateSet(iterable));
}

/**
* Finds and returns the sequenced Set of duplicate elements in the given collection.
* <p>
* Once we are on Java 21 and a new major version, the return type should be SequencedSet.
* </p>
*
* @param <E> the type of elements in the collection.
* @param iterable the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
* @since 4.5.0-M3
*/
public static <E> Set<E> duplicateSequencedSet(final Iterable<E> iterable) {
return duplicateSet(iterable, new LinkedHashSet<>());
}

/**
* Finds and returns the set of duplicate elements in the given collection.
*
* @param <E> the type of elements in the collection.
* @param iterable the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
* @since 4.5.0-M3
*/
public static <E> Set<E> duplicateSet(final Iterable<E> iterable) {
return duplicateSet(iterable, new HashSet<>());
}

/**
* Worker method for {@link #duplicateSet(Collection)} and friends.
*
* @param <C> the type of Collection.
* @param <E> the type of elements in the Collection.
* @param iterable the list to test, must not be null.
* @param duplicates the list to test, must not be null.
* @return the set of duplicate elements, may be empty.
*/
static <C extends Collection<E>, E> C duplicateSet(final Iterable<E> iterable, final C duplicates) {
final Set<E> set = new HashSet<>();
for (final E e : iterable) {
(set.contains(e) ? duplicates : set).add(e);
}
return duplicates;
}

/**
* Returns an immutable empty iterable if the argument is null,
* or the argument itself otherwise.
Expand Down
134 changes: 0 additions & 134 deletions src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -876,137 +873,6 @@ public void testDisjunctionNullColl2() {
assertThrows(NullPointerException.class, () -> CollectionUtils.disjunction(list, null));
}

@Test
public void testDuplicateListAllSameInList() {
final List<Integer> input = Arrays.asList(5, 5, 5, 5);
assertEquals(Arrays.asList(5), CollectionUtils.duplicateList(input));
}

@Test
public void testDuplicateListEmptyDeque() {
assertTrue(CollectionUtils.duplicateList(new ArrayDeque<>()).isEmpty());
}

@Test
public void testDuplicateListEmptyList() {
final List<Integer> input = Arrays.asList();
assertTrue(CollectionUtils.duplicateList(input).isEmpty());
}

@Test
public void testDuplicateListEmptySet() {
assertTrue(CollectionUtils.duplicateList(new HashSet<>()).isEmpty());
}

@Test
public void testDuplicateListMultipleDuplicatesInDeque() {
final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4));
final List<Integer> expected = Arrays.asList(1, 2, 3, 4);
assertEquals(expected, CollectionUtils.duplicateList(input));
}

@Test
public void testDuplicateListMultipleDuplicatesInList() {
final List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
final List<Integer> expected = Arrays.asList(1, 2, 3, 4);
assertEquals(expected, CollectionUtils.duplicateList(input));
}

@Test
public void testDuplicateListNoDuplicates() {
final List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
assertTrue(CollectionUtils.duplicateList(input).isEmpty());
}

@Test
public void testDuplicateListSingleElement() {
final List<Integer> input = Arrays.asList(1);
assertTrue(CollectionUtils.duplicateList(input).isEmpty());
}

@Test
public void testDuplicateListWithDuplicates() {
final List<Integer> input = Arrays.asList(1, 2, 3, 2, 4, 5, 3);
final List<Integer> expected = Arrays.asList(2, 3);
assertEquals(expected, CollectionUtils.duplicateList(input));
}

@Test
public void testDuplicateSequencedSetMultipleDuplicates() {
final List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
final List<Integer> list = Arrays.asList(1, 2, 3, 4);
assertEquals(list, new ArrayList<>(CollectionUtils.duplicateSequencedSet(input)));
assertEquals(new LinkedHashSet<>(list), CollectionUtils.duplicateSequencedSet(input));
}

@Test
public void testDuplicateSetEmptyDeque() {
assertTrue(CollectionUtils.duplicateSet(new ArrayDeque<>()).isEmpty());
}

@Test
public void testDuplicateSetEmptyList() {
final List<Integer> input = Arrays.asList();
assertTrue(CollectionUtils.duplicateSet(input).isEmpty());
}

@Test
public void testDuplicateSetEmptySet() {
assertTrue(CollectionUtils.duplicateSet(new HashSet<>()).isEmpty());
}

@Test
public void testDuplicateSetInSet() {
// Sets don't have duplicates, so the result is always an empty set.
final Set<Integer> input = new HashSet<>(Arrays.asList(5));
assertTrue(CollectionUtils.duplicateSet(input).isEmpty());
}

@Test
public void testDuplicateSetMultipleDuplicatesInDeque() {
final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4));
final Set<Integer> expected = new HashSet<>(Arrays.asList(1, 2, 3, 4));
assertEquals(expected, CollectionUtils.duplicateSet(input));
}

@Test
public void testDuplicateSetMultipleDuplicatesInList() {
final List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
final Set<Integer> expected = new HashSet<>(Arrays.asList(1, 2, 3, 4));
assertEquals(expected, CollectionUtils.duplicateSet(input));
}

@Test
public void testDuplicateSetNoDuplicates() {
final List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
assertTrue(CollectionUtils.duplicateSet(input).isEmpty());
}

@Test
public void testDuplicateSetSingleElement() {
final List<Integer> input = Arrays.asList(1);
assertTrue(CollectionUtils.duplicateSet(input).isEmpty());
}

@Test
public void testDuplicateSetWithDuplicates() {
final List<Integer> input = Arrays.asList(1, 2, 3, 2, 4, 5, 3);
final Set<Integer> expected = new HashSet<>(Arrays.asList(2, 3));
assertEquals(expected, CollectionUtils.duplicateSet(input));
}

@Test
public void testDuplicatListAllSameInDeque() {
final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(5, 5, 5, 5));
assertEquals(Arrays.asList(5), CollectionUtils.duplicateList(input));
}

@Test
public void testDuplicatSetAllSameInDeque() {
final Deque<Integer> input = new ArrayDeque<>(Arrays.asList(5, 5, 5, 5));
assertEquals(new HashSet<>(Arrays.asList(5)), CollectionUtils.duplicateSet(input));
}

@Test
public void testEmptyCollection() throws Exception {
final Collection<Number> coll = CollectionUtils.emptyCollection();
Expand Down
Loading

0 comments on commit 053d2dd

Please sign in to comment.