Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findDuplicates Method to ListUtils with Comprehensive Test Cases #537

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
91e76c8
Add findDuplicates method to ListUtils with comprehensive test cases
hemanth0525 Aug 31, 2024
e12fc22
findDuplicates
hemanth0525 Sep 1, 2024
53c385d
updated tests
hemanth0525 Sep 1, 2024
8a2808a
Update tests
hemanth0525 Sep 1, 2024
0f26985
Merge branch 'apache:master' into master
hemanth0525 Sep 1, 2024
53f249d
Merge branch 'apache:master' into feature/findDuplicates
hemanth0525 Sep 1, 2024
c455e6a
Add findDuplicates
hemanth0525 Sep 1, 2024
f5c7e46
Merge branch 'apache:master' into feature/findDuplicates
hemanth0525 Sep 2, 2024
6ebffeb
Add findDuplicates
hemanth0525 Sep 2, 2024
9197ae3
Add findDuplicates
hemanth0525 Sep 1, 2024
57e86e3
Bump org.easymock:easymock from 5.3.0 to 5.4.0
dependabot[bot] Sep 1, 2024
a465792
Bump org.easymock:easymock from 5.3.0 to 5.4.0 #521
garydgregory Sep 1, 2024
2081cbe
Whitespace
garydgregory Sep 1, 2024
ff20df6
Use Java 8's Map.getOrDefault()
garydgregory Sep 1, 2024
2adf5ee
Reimplement the package-private CollectionUtils.CardinalityHelper using
garydgregory Sep 1, 2024
40dc0aa
Add HashBag.HashBag(Iterable)
garydgregory Sep 1, 2024
94c51b3
[COLLECTIONS-858] CartesianProductIterator
alexey-pelykh Jun 25, 2024
eb748a3
Add CartesianProductIterator #509
garydgregory Sep 1, 2024
740ed3a
Javadoc
garydgregory Sep 1, 2024
3c991c0
Inline redundant package-private method that wraps
garydgregory Sep 1, 2024
6e273e7
Use Assertions.assertInstanceOf()
garydgregory Sep 1, 2024
1036610
Add findDuplicates
hemanth0525 Sep 2, 2024
dd78ec6
Merge branch 'feature/findDuplicates' of https://github.com/hemanth05…
hemanth0525 Sep 2, 2024
3137d22
Merge branch 'apache:master' into master
hemanth0525 Sep 2, 2024
053d2dd
Merge branch 'master' of https://github.com/apache/commons-collection…
hemanth0525 Sep 2, 2024
0e70f14
Add findDuplicates
hemanth0525 Sep 2, 2024
cb62f7a
Merge branch 'apache:master' into master
hemanth0525 Sep 3, 2024
e915879
Merge branch 'apache:master' into feature/findDuplicates
hemanth0525 Sep 4, 2024
277063f
Add diverse test cases for IterableUtils.duplicateList:
hemanth0525 Sep 4, 2024
3eaeeb6
Merge branch 'master' of https://github.com/hemanth0525/commons-colle…
hemanth0525 Sep 4, 2024
6423bf3
Corrected spaces and lines
hemanth0525 Sep 5, 2024
1f6ce8b
Merge branch 'apache:master' into feature/findDuplicates
hemanth0525 Sep 8, 2024
bcb765b
Add more Comprehensive tests IterableUtils.duplicateList
hemanth0525 Sep 8, 2024
f1b2f22
Merge branch 'feature/findDuplicates' of https://github.com/hemanth05…
hemanth0525 Sep 8, 2024
12dccf9
remove duplicate imports
hemanth0525 Sep 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/org/apache/commons/collections4/ListUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.collections4.bag.HashBag;
import org.apache.commons.collections4.functors.DefaultEquator;
Expand Down Expand Up @@ -173,6 +174,39 @@ public static <E> List<E> fixedSizeList(final List<E> list) {
return FixedSizeList.fixedSizeList(list);
}

/**
* Finds and returns a list of duplicate elements in the given list.
* <p>
* This method uses two sets: one for tracking seen elements and one for
* collecting duplicates. It iterates through the list once and collects
* duplicates in a result list.
* </p>
*
* @param <E> the type of elements in the list
* @param list the list to check for duplicates, must not be null
* @return a list of duplicate elements, or an empty list if no duplicates are found
* @throws NullPointerException if the list is null
*/
hemanth0525 marked this conversation as resolved.
Show resolved Hide resolved
public static <E> List<E> findDuplicates(final List<E> list) {
if (list == null) {
hemanth0525 marked this conversation as resolved.
Show resolved Hide resolved
throw new NullPointerException("The input list must not be null.");
}

Set<E> seen = new HashSet<>();
Set<E> duplicates = new HashSet<>();
List<E> result = new ArrayList<>();

for (E element : list) {
if (!seen.add(element)) {
if (duplicates.add(element)) {
result.add(element);
}
}
}

return result;
}

/**
* Gets the first element of a list.
* <p>
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/apache/commons/collections4/ListUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,54 @@ public void testEquals() {
assertFalse(ListUtils.isEqualList(list1, list2));
}

@Test
public void testFindDuplicatesWithDuplicates() {
List<Integer> input = Arrays.asList(1, 2, 3, 2, 4, 5, 3);
List<Integer> expected = Arrays.asList(2, 3);
List<Integer> actual = ListUtils.findDuplicates(input);
assertEquals(expected, actual, "The list should contain only the duplicate elements.");
}

@Test
public void testFindDuplicatesNoDuplicates() {
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expected = Arrays.asList();
List<Integer> actual = ListUtils.findDuplicates(input);
assertEquals(expected, actual, "The list should be empty as there are no duplicates.");
}

@Test
public void testFindDuplicatesEmptyList() {
List<Integer> input = Arrays.asList();
List<Integer> expected = Arrays.asList();
List<Integer> actual = ListUtils.findDuplicates(input);
assertEquals(expected, actual, "The list should be empty as the input list is empty.");
}

@Test
public void testFindDuplicatesSingleElement() {
List<Integer> input = Arrays.asList(1);
List<Integer> expected = Arrays.asList();
List<Integer> actual = ListUtils.findDuplicates(input);
assertEquals(expected, actual, "The list should be empty as there is only one element.");
hemanth0525 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testFindDuplicatesMultipleDuplicates() {
List<Integer> input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4);
List<Integer> expected = Arrays.asList(1, 2, 3, 4);
List<Integer> actual = ListUtils.findDuplicates(input);
assertEquals(expected, actual, "The list should contain all duplicate elements.");
}

@Test
public void testFindDuplicatesAllSameElements() {
List<Integer> input = Arrays.asList(5, 5, 5, 5);
List<Integer> expected = Arrays.asList(5);
List<Integer> actual = ListUtils.findDuplicates(input);
assertEquals(expected, actual, "The list should contain the single element as all elements are the same.");
}

@Test
public void testGetFirst() {
assertEquals(a, ListUtils.getFirst(fullList));
Expand Down
Loading