|
3 | 3 | import static org.hamcrest.Matchers.hasSize; |
4 | 4 | import static org.junit.Assert.assertThat; |
5 | 5 |
|
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashSet; |
6 | 8 | import java.util.List; |
7 | 9 |
|
8 | 10 | import org.apache.commons.collections4.CollectionUtils; |
|
12 | 14 | import com.google.common.base.Predicates; |
13 | 15 | import com.google.common.collect.Iterables; |
14 | 16 | import com.google.common.collect.Lists; |
| 17 | +import com.google.common.collect.Sets; |
15 | 18 |
|
16 | 19 | public class JavaCollectionCleanupUnitTest { |
17 | 20 |
|
18 | | - // removing nulls |
| 21 | + // tests - removing nulls |
19 | 22 |
|
20 | 23 | @Test |
21 | 24 | public final void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() { |
@@ -50,4 +53,22 @@ public final void givenListContainsNulls_whenRemovingNullsWithCommonsCollections |
50 | 53 | assertThat(list, hasSize(3)); |
51 | 54 | } |
52 | 55 |
|
| 56 | + // tests - remove duplicates |
| 57 | + |
| 58 | + @Test |
| 59 | + public final void givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect() { |
| 60 | + final List<Integer> listWithDuplicates = Lists.newArrayList(0, 1, 2, 3, 0, 0); |
| 61 | + final List<Integer> listWithoutDuplicates = new ArrayList<>(new HashSet<>(listWithDuplicates)); |
| 62 | + |
| 63 | + assertThat(listWithoutDuplicates, hasSize(4)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public final void givenListContainsDuplicates_whenRemovingDuplicatesWithGuava_thenCorrect() { |
| 68 | + final List<Integer> listWithDuplicates = Lists.newArrayList(0, 1, 2, 3, 0, 0); |
| 69 | + final List<Integer> listWithoutDuplicates = Lists.newArrayList(Sets.newHashSet(listWithDuplicates)); |
| 70 | + |
| 71 | + assertThat(listWithoutDuplicates, hasSize(4)); |
| 72 | + } |
| 73 | + |
53 | 74 | } |
0 commit comments