[BAEL-2170] Differences between ArrayList clear and removeAll

This commit is contained in:
dupirefr 2018-09-06 20:15:23 +02:00
parent f9277b37fe
commit cc0749ce8a
1 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.baeldung.list.arraylist;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll()
*/
class ClearVsRemoveAllTest {
/*
* Tests
*/
@Test
void givenArrayListWithElements_whenClear_thenListBecomesEmpty() {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
list.clear();
assertTrue(list.isEmpty());
}
@Test
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
ArrayList<Integer> firstList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
ArrayList<Integer> secondList = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
firstList.removeAll(secondList);
assertEquals(Arrays.asList(1, 2), firstList);
}
}