Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ctions into feature/findDuplicates
  • Loading branch information
hemanth0525 committed Sep 4, 2024
2 parents e915879 + 277063f commit 3eaeeb6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 62 deletions.
19 changes: 0 additions & 19 deletions src/main/java/org/apache/commons/collections4/ListUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,6 @@ public static <T> List<T> emptyIfNull(final List<T> list) {
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
* @since 4.5.0-M3
*/
public static <E> List<E> findDuplicates(final List<E> list) {
return IterableUtils.duplicateList(list);
}

/**
* Gets the first element of a list.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.collections4.bag.HashBag;
Expand Down Expand Up @@ -300,6 +301,92 @@ public void testDuplicatSetAllSameInDeque() {
assertEquals(new HashSet<>(Arrays.asList(5)), IterableUtils.duplicateSet(input));
}

@Test
public void testDuplicateListMixedElementTypes() {
final List<Object> input = Arrays.asList(1, "a", 2, "a", 3, "b", "b");
final List<Object> expected = Arrays.asList("a", "b");
assertEquals(expected, IterableUtils.duplicateList(input));
}

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

@Test
public void testDuplicateListCustomObjects() {
class Person {
String name;
int age;

Person(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Person person = (Person) o;
return age == person.age && name.equals(person.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age);
}
}

final List<Person> input = Arrays.asList(
new Person("Linus", 30),
new Person("Torvalds", 25),
new Person("Linus", 30),
new Person("Tim", 35),
new Person("Torvalds", 25)
);
final List<Person> expected = Arrays.asList(
new Person("Linus", 30),
new Person("Torvalds", 25)
);
assertEquals(expected, IterableUtils.duplicateList(input));
}

@Test
public void testDuplicateListLargeScaleData() {
final List<Integer> input = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
input.add(i % 100);
}
final List<Integer> expected = new ArrayList<>();
for (int i = 0; i < 100; i++) {
expected.add(i);
}
final List<Integer> actual = IterableUtils.duplicateList(input);
assertEquals(expected, actual);
}

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

@Test
public void testFind() {
Predicate<Number> testPredicate = equalPredicate(4);
Expand Down
43 changes: 0 additions & 43 deletions src/test/java/org/apache/commons/collections4/ListUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,49 +103,6 @@ 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);
assertTrue(ListUtils.findDuplicates(input).isEmpty(), "The list should be empty as the input list has no duplicates.");
}

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

@Test
public void testFindDuplicatesSingleElement() {
List<Integer> input = Arrays.asList(1);
assertTrue(ListUtils.findDuplicates(input).isEmpty(), "The list should be empty as there is only one element.");
}


@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

0 comments on commit 3eaeeb6

Please sign in to comment.