garydgregory commented on code in PR #537:
URL:
https://github.com/apache/commons-collections/pull/537#discussion_r1740092109
##########
src/main/java/org/apache/commons/collections4/ListUtils.java:
##########
@@ -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
+ */
Review Comment:
Add `@since 4.5.0-M3`
##########
src/test/java/org/apache/commons/collections4/ListUtilsTest.java:
##########
@@ -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.");
Review Comment:
Empty results can be simply asserted as
`assertTrue(ListUtils.duplicates(input).isEmpty());`
I'm going to create a CollectionUtils version of the method called
`duplicates`, I suggest the method be named the same here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]