[BAEL-4495] Performance of removeAll() in a HashSet (#10011)

* [BAEL-4495] Performance of removeAll() in a HashSet

* [BAEL-4495] Add unit test for hashset removeAll()

* [BAEL-4495] Update test methods to camelCase
This commit is contained in:
AmitB
2020-09-15 10:14:25 +05:30
committed by GitHub
parent 73249a9652
commit 9725f52fad
2 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package com.baeldung.collections.hashset;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
class HashSetUnitTest {
@Test
void whenRemoveAllFromHashset_thenRemovesAllElementsFromHashsetThatArePresentInCollection() {
Set<Integer> set = new HashSet<>();
Collection<Integer> collection = new ArrayList<>();
set.add(1);
set.add(2);
set.add(3);
set.add(4);
collection.add(1);
collection.add(3);
set.removeAll(collection);
assertEquals(2, set.size());
Integer[] actualElements = new Integer[set.size()];
Integer[] expectedElements = new Integer[] { 2, 4 };
assertArrayEquals(expectedElements, set.toArray(actualElements));
}
}