Skip to content

Commit cc0749c

Browse files
author
dupirefr
committed
[BAEL-2170] Differences between ArrayList clear and removeAll
1 parent f9277b3 commit cc0749c

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)