Skip to content

Commit 1aa2340

Browse files
authored
Merge pull request eugenp#5182 from FDPro/master
dupirefr/dupire.francois+pro@gmail.com [BAEL-2170] Differences between ArrayList clear and removeAll
2 parents 3bdc573 + 69c6c30 commit 1aa2340

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.collection;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertTrue;
11+
12+
/**
13+
* Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll()
14+
*/
15+
class ClearVsRemoveAllUnitTest {
16+
17+
/*
18+
* Tests
19+
*/
20+
@Test
21+
void givenArrayListWithElements_whenClear_thenListBecomesEmpty() {
22+
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
23+
24+
collection.clear();
25+
26+
assertTrue(collection.isEmpty());
27+
}
28+
29+
@Test
30+
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
31+
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
32+
Collection<Integer> secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
33+
34+
firstCollection.removeAll(secondCollection);
35+
36+
assertEquals(Arrays.asList(1, 2), firstCollection);
37+
assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)