From 9725f52fad7b336ff7f44d57ba9b5d648f63a6d1 Mon Sep 17 00:00:00 2001 From: AmitB Date: Tue, 15 Sep 2020 10:14:25 +0530 Subject: [PATCH] [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 --- .../HashSetBenchmark.java | 86 +++++++++++++++++++ .../collections/hashset/HashSetUnitTest.java | 33 +++++++ 2 files changed, 119 insertions(+) create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/removeallperformance/HashSetBenchmark.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/hashset/HashSetUnitTest.java diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/removeallperformance/HashSetBenchmark.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/removeallperformance/HashSetBenchmark.java new file mode 100644 index 0000000000..8ce58c865e --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/removeallperformance/HashSetBenchmark.java @@ -0,0 +1,86 @@ +package com.baeldung.collections.removeallperformance; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang3.RandomStringUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import com.baeldung.collections.containsperformance.Employee; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 5) +public class HashSetBenchmark { + + @State(Scope.Thread) + public static class MyState { + private Set employeeSet1 = new HashSet<>(); + private List employeeList1 = new ArrayList<>(); + private Set employeeSet2 = new HashSet<>(); + private List employeeList2 = new ArrayList<>(); + + private long set1Size = 60000; + private long list1Size = 50000; + private long set2Size = 50000; + private long list2Size = 60000; + + @Setup(Level.Trial) + public void setUp() { + + for (long i = 0; i < set1Size; i++) { + employeeSet1.add(new Employee(i, RandomStringUtils.random(7, true, false))); + } + + for (long i = 0; i < list1Size; i++) { + employeeList1.add(new Employee(i, RandomStringUtils.random(7, true, false))); + } + + for (long i = 0; i < set2Size; i++) { + employeeSet2.add(new Employee(i, RandomStringUtils.random(7, true, false))); + } + + for (long i = 0; i < list2Size; i++) { + employeeList2.add(new Employee(i, RandomStringUtils.random(7, true, false))); + } + + } + + } + + @Benchmark + public boolean given_SizeOfHashsetGreaterThanSizeOfCollection_When_RemoveAllFromHashSet_Then_GoodPerformance(MyState state) { + return state.employeeSet1.removeAll(state.employeeList1); + } + + @Benchmark + public boolean given_SizeOfHashsetSmallerThanSizeOfCollection_When_RemoveAllFromHashSet_Then_BadPerformance(MyState state) { + return state.employeeSet2.removeAll(state.employeeList2); + } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder().include(HashSetBenchmark.class.getSimpleName()) + .threads(1) + .forks(1) + .shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server") + .build(); + new Runner(options).run(); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/hashset/HashSetUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/hashset/HashSetUnitTest.java new file mode 100644 index 0000000000..1c23538675 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/hashset/HashSetUnitTest.java @@ -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 set = new HashSet<>(); + Collection 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)); + } + +}