From 0d169bb5166898a47bf3eae2a95f36d780f0f3be Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Tue, 3 May 2022 11:59:45 -0400 Subject: [PATCH] =?UTF-8?q?core-java-collections-3=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=B0=E4=BB=93=E5=BA=93=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core-java-collections-3/README.md | 17 ++ .../core-java-collections-3/pom.xml | 40 ++++ .../arraylistvsvector/ArrayListBenchmark.java | 90 +++++++++ .../arraylistvsvector/Employee.java | 55 ++++++ .../arraylistvsvector/VectorExample.java | 27 +++ .../CollectionsBenchmark.java | 59 ++++++ .../containsperformance/Employee.java | 55 ++++++ .../collections/iterators/Iterators.java | 79 ++++++++ .../HashSetBenchmark.java | 102 ++++++++++ .../sortingcomparison/ArraySortBenchmark.java | 51 +++++ .../CollectionsSortCompare.java | 39 ++++ .../collections/bitset/BitSetUnitTest.java | 182 +++++++++++++++++ .../ClearVsRemoveAllUnitTest.java | 40 ++++ .../HashmapVsHashtableDifferenceUnitTest.java | 98 ++++++++++ .../collections/hashset/HashSetUnitTest.java | 33 ++++ .../iterators/IteratorsUnitTest.java | 40 ++++ .../mapfirstpair/MapFirstPairUnitTest.java | 146 ++++++++++++++ .../collections/stack/StackUnitTest.java | 183 ++++++++++++++++++ .../src/test/resources/logback.xml | 13 ++ 19 files changed, 1349 insertions(+) create mode 100644 core-java-modules/core-java-collections-3/README.md create mode 100644 core-java-modules/core-java-collections-3/pom.xml create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/ArrayListBenchmark.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/Employee.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/VectorExample.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/CollectionsBenchmark.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/Employee.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/iterators/Iterators.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/removeallperformance/HashSetBenchmark.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/ArraySortBenchmark.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/CollectionsSortCompare.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/bitset/BitSetUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/clearvsremoveall/ClearVsRemoveAllUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashmapvshashtable/HashmapVsHashtableDifferenceUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashset/HashSetUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/iterators/IteratorsUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/mapfirstpair/MapFirstPairUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/stack/StackUnitTest.java create mode 100644 core-java-modules/core-java-collections-3/src/test/resources/logback.xml diff --git a/core-java-modules/core-java-collections-3/README.md b/core-java-modules/core-java-collections-3/README.md new file mode 100644 index 0000000000..4249d8ad30 --- /dev/null +++ b/core-java-modules/core-java-collections-3/README.md @@ -0,0 +1,17 @@ +========= + +## Core Java Collections Cookbooks and Examples + +### Relevant Articles: + +- [Time Comparison of Arrays.sort(Object[]) and Arrays.sort(int[])](https://www.baeldung.com/arrays-sortobject-vs-sortint) +- [Java ArrayList vs Vector](https://www.baeldung.com/java-arraylist-vs-vector) +- [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences) +- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall) +- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance) +- [Fail-Safe Iterator vs Fail-Fast Iterator](https://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator) +- [Quick Guide to the Java Stack](https://www.baeldung.com/java-stack) +- [A Guide to BitSet in Java](https://www.baeldung.com/java-bitset) +- [Get the First Key and Value From a HashMap](https://www.baeldung.com/java-hashmap-get-first-entry) +- [Performance of removeAll() in a HashSet](https://www.baeldung.com/java-hashset-removeall-performance) +- More articles: [[<-- prev]](/core-java-modules/core-java-collections-2) [[next -->]](/core-java-modules/core-java-collections-4) diff --git a/core-java-modules/core-java-collections-3/pom.xml b/core-java-modules/core-java-collections-3/pom.xml new file mode 100644 index 0000000000..28c80f9078 --- /dev/null +++ b/core-java-modules/core-java-collections-3/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + core-java-collections-3 + 0.1.0-SNAPSHOT + core-java-collections-3 + jar + + + com.ossez.core-java-modules + core-java-modules + 0.0.2-SNAPSHOT + ../pom.xml + + + + + org.openjdk.jol + jol-core + ${jol-core.version} + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + 0.10 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/ArrayListBenchmark.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/ArrayListBenchmark.java new file mode 100644 index 0000000000..850b98c137 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/ArrayListBenchmark.java @@ -0,0 +1,90 @@ +package com.ossez.collections.collections.arraylistvsvector; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 10) +public class ArrayListBenchmark { + + @State(Scope.Thread) + public static class MyState { + + List employeeList = new ArrayList<>(); + Vector employeeVector = new Vector<>(); + //LinkedList employeeList = new LinkedList<>(); + + long iterations = 100000; + + Employee employee = new Employee(100L, "Harry"); + + int employeeIndex = -1; + + @Setup(Level.Trial) + public void setUp() { + for (long i = 0; i < iterations; i++) { + employeeList.add(new Employee(i, "John")); + employeeVector.add(new Employee(i, "John")); + } + + employeeList.add(employee); + employeeVector.add(employee); + employeeIndex = employeeList.indexOf(employee); + } + } + + @Benchmark + public void testAddAt(ArrayListBenchmark.MyState state) { + state.employeeList.add((int) (state.iterations), new Employee(state.iterations, "John")); + } + + @Benchmark + public boolean testContains(ArrayListBenchmark.MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public boolean testContainsVector(ArrayListBenchmark.MyState state) { + return state.employeeVector.contains(state.employee); + } + + @Benchmark + public int testIndexOf(ArrayListBenchmark.MyState state) { + return state.employeeList.indexOf(state.employee); + } + + @Benchmark + public Employee testGet(ArrayListBenchmark.MyState state) { + return state.employeeList.get(state.employeeIndex); + } + + @Benchmark + public Employee testVectorGet(ArrayListBenchmark.MyState state) { + return state.employeeVector.get(state.employeeIndex); + } + + @Benchmark + public boolean testRemove(ArrayListBenchmark.MyState state) { + return state.employeeList.remove(state.employee); + } + + @Benchmark + public void testAdd(ArrayListBenchmark.MyState state) { + state.employeeList.add(new Employee(state.iterations + 1, "John")); + } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(ArrayListBenchmark.class.getSimpleName()).threads(3) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/Employee.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/Employee.java new file mode 100644 index 0000000000..a271f455fe --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/Employee.java @@ -0,0 +1,55 @@ +package com.ossez.collections.collections.arraylistvsvector; + +public class Employee { + + private Long id; + private String name; + + public Employee(Long id, String name) { + this.name = name; + this.id = id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Employee employee = (Employee) o; + + if (!id.equals(employee.id)) return false; + return name.equals(employee.name); + + } + + @Override + public int hashCode() { + int result = id.hashCode(); + result = 31 * result + name.hashCode(); + return result; + } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/VectorExample.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/VectorExample.java new file mode 100644 index 0000000000..d38b23ab74 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/arraylistvsvector/VectorExample.java @@ -0,0 +1,27 @@ +package com.ossez.collections.collections.arraylistvsvector; + +import java.util.Enumeration; +import java.util.Iterator; +import java.util.Vector; + +public class VectorExample { + + public static void main(String[] args) { + + Vector vector = new Vector<>(); + vector.add("baeldung"); + vector.add("Vector"); + vector.add("example"); + + Enumeration e = vector.elements(); + while(e.hasMoreElements()){ + System.out.println(e.nextElement()); + } + + Iterator iterator = vector.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/CollectionsBenchmark.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/CollectionsBenchmark.java new file mode 100644 index 0000000000..a7ed3c12e6 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/CollectionsBenchmark.java @@ -0,0 +1,59 @@ +package com.ossez.collections.collections.containsperformance; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 5) +public class CollectionsBenchmark { + + @State(Scope.Thread) + public static class MyState { + private Set employeeSet = new HashSet<>(); + private List employeeList = new ArrayList<>(); + + private long iterations = 10000; + + private Employee employee = new Employee(100L, "Harry"); + + @Setup(Level.Trial) + public void setUp() { + + for (long i = 0; i < iterations; i++) { + employeeSet.add(new Employee(i, "John")); + employeeList.add(new Employee(i, "John")); + } + + employeeList.add(employee); + employeeSet.add(employee); + } + } + + @Benchmark + public boolean testArrayList(MyState state) { + return state.employeeList.contains(state.employee); + } + + @Benchmark + public boolean testHashSet(MyState state) { + return state.employeeSet.contains(state.employee); + } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(CollectionsBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/Employee.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/Employee.java new file mode 100644 index 0000000000..4e9bd3c215 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/containsperformance/Employee.java @@ -0,0 +1,55 @@ +package com.ossez.collections.collections.containsperformance; + +public class Employee { + + private Long id; + private String name; + + public Employee(Long id, String name) { + this.name = name; + this.id = id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Employee employee = (Employee) o; + + if (!id.equals(employee.id)) return false; + return name.equals(employee.name); + + } + + @Override + public int hashCode() { + int result = id.hashCode(); + result = 31 * result + name.hashCode(); + return result; + } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/iterators/Iterators.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/iterators/Iterators.java new file mode 100644 index 0000000000..a98c2b6ff9 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/iterators/Iterators.java @@ -0,0 +1,79 @@ +package com.ossez.collections.collections.iterators; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.concurrent.ConcurrentHashMap; + +public class Iterators { + + private static final Logger LOG = LoggerFactory.getLogger(Iterators.class); + + public static int failFast1() { + ArrayList numbers = new ArrayList<>(); + + numbers.add(10); + numbers.add(20); + numbers.add(30); + numbers.add(40); + + Iterator iterator = numbers.iterator(); + while (iterator.hasNext()) { + Integer number = iterator.next(); + numbers.add(50); + } + + return numbers.size(); + } + + public static int failFast2() { + ArrayList numbers = new ArrayList<>(); + numbers.add(10); + numbers.add(20); + numbers.add(30); + numbers.add(40); + + Iterator iterator = numbers.iterator(); + while (iterator.hasNext()) { + if (iterator.next() == 30) { + // will not throw Exception + iterator.remove(); + } + } + + LOG.debug("using iterator's remove method = {}", numbers); + + iterator = numbers.iterator(); + while (iterator.hasNext()) { + if (iterator.next() == 40) { + // will throw Exception on + // next call of next() method + numbers.remove(2); + } + } + + return numbers.size(); + } + + public static int failSafe1() { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + + map.put("First", 10); + map.put("Second", 20); + map.put("Third", 30); + map.put("Fourth", 40); + + Iterator iterator = map.keySet() + .iterator(); + + while (iterator.hasNext()) { + String key = iterator.next(); + map.put("Fifth", 50); + } + + return map.size(); + } + +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/removeallperformance/HashSetBenchmark.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/removeallperformance/HashSetBenchmark.java new file mode 100644 index 0000000000..29a692e4de --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/removeallperformance/HashSetBenchmark.java @@ -0,0 +1,102 @@ +package com.ossez.collections.collections.removeallperformance; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import com.ossez.collections.collections.containsperformance.Employee; +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; + +@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 Set employeeSet3 = new HashSet<>(); + private Set employeeSet4 = new HashSet<>(); + + private long set1Size = 60000; + private long list1Size = 50000; + private long set2Size = 50000; + private long list2Size = 60000; + private long set3Size = 50000; + private long set4Size = 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))); + } + + for (long i = 0; i < set3Size; i++) { + employeeSet3.add(new Employee(i, RandomStringUtils.random(7, true, false))); + } + + for (long i = 0; i < set4Size; i++) { + employeeSet4.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); + } + + @Benchmark + public boolean given_SizeOfHashsetSmallerThanSizeOfAnotherHashSet_When_RemoveAllFromHashSet_Then_GoodPerformance(MyState state) { + return state.employeeSet3.removeAll(state.employeeSet4); + } + + 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/main/java/com/ossez/collections/sortingcomparison/ArraySortBenchmark.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/ArraySortBenchmark.java new file mode 100644 index 0000000000..651fe06d14 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/ArraySortBenchmark.java @@ -0,0 +1,51 @@ +package com.ossez.collections.collections.sortingcomparison; + +import java.util.Arrays; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +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; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Measurement(batchSize = 100000, iterations = 10) +@Warmup(batchSize = 100000, iterations = 10) +public class ArraySortBenchmark { + + @State(Scope.Thread) + public static class Initialize { + Integer[] numbers = { -769214442, -1283881723, 1504158300, -1260321086, -1800976432, 1278262737, 1863224321, 1895424914, 2062768552, -1051922993, 751605209, -1500919212, 2094856518, -1014488489, -931226326, -1677121986, -2080561705, 562424208, -1233745158, 41308167 }; + int[] primitives = { -769214442, -1283881723, 1504158300, -1260321086, -1800976432, 1278262737, 1863224321, 1895424914, 2062768552, -1051922993, 751605209, -1500919212, 2094856518, -1014488489, -931226326, -1677121986, -2080561705, 562424208, -1233745158, 41308167 }; + } + + @Benchmark + public Integer[] benchmarkArraysIntegerSort(ArraySortBenchmark.Initialize state) { + Arrays.sort(state.numbers); + return state.numbers; + } + + @Benchmark + public int[] benchmarkArraysIntSort(ArraySortBenchmark.Initialize state) { + Arrays.sort(state.primitives); + return state.primitives; + } + + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(ArraySortBenchmark.class.getSimpleName()).threads(1) + .forks(1).shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/CollectionsSortCompare.java b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/CollectionsSortCompare.java new file mode 100644 index 0000000000..dc82c8fac7 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/ossez/collections/sortingcomparison/CollectionsSortCompare.java @@ -0,0 +1,39 @@ +package com.ossez.collections.collections.sortingcomparison; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class CollectionsSortCompare { + + public static void main(String[] args) { + sortPrimitives(); + sortReferenceType(); + sortCollection(); + } + + private static void sortReferenceType() { + Integer[] numbers = {5, 22, 10, 0}; + Arrays.sort(numbers); + System.out.println(Arrays.toString(numbers)); + } + + private static void sortCollection() { + List numbersList = new ArrayList<>(); + numbersList.add(5); + numbersList.add(22); + numbersList.add(10); + numbersList.add(0); + + Collections.sort(numbersList); + + numbersList.forEach(System.out::print); + } + + private static void sortPrimitives() { + int[] numbers = {5, 22, 10, 0}; + Arrays.sort(numbers); + System.out.println(Arrays.toString(numbers)); + } +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/bitset/BitSetUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/bitset/BitSetUnitTest.java new file mode 100644 index 0000000000..f65bdc5fd3 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/bitset/BitSetUnitTest.java @@ -0,0 +1,182 @@ +package com.ossez.collections.bitset; + +import org.junit.Test; +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.info.GraphLayout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.BitSet; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BitSetUnitTest { + + private static final Logger LOG = LoggerFactory.getLogger(BitSetUnitTest.class); + + @Test + public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() { + boolean[] bits = new boolean[1024 * 1024]; + + LOG.debug(ClassLayout.parseInstance(bits).toPrintable()); + } + + @Test + public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() { + BitSet bitSet = new BitSet(1024 * 1024); + + LOG.debug(GraphLayout.parseInstance(bitSet).toPrintable()); + } + + @Test + public void givenBitSet_whenSetting_thenShouldBeTrue() { + BitSet bitSet = new BitSet(); + + bitSet.set(10); + assertThat(bitSet.get(10)).isTrue(); + + bitSet.set(20, 30); + for (int i = 20; i <= 29; i++) { + assertThat(bitSet.get(i)).isTrue(); + } + assertThat(bitSet.get(30)).isFalse(); + + bitSet.set(10, false); + assertThat(bitSet.get(10)).isFalse(); + + bitSet.set(20, 30, false); + for (int i = 20; i <= 30; i++) { + assertThat(bitSet.get(i)).isFalse(); + } + } + + @Test + public void givenBitSet_whenClearing_thenShouldBeFalse() { + BitSet bitSet = new BitSet(); + bitSet.set(42); + assertThat(bitSet.get(42)).isTrue(); + + bitSet.clear(42); + assertThat(bitSet.get(42)).isFalse(); + + bitSet.set(10, 20); + for (int i = 10; i < 20; i++) { + assertThat(bitSet.get(i)).isTrue(); + } + + bitSet.clear(10, 20); + for (int i = 10; i < 20; i++) { + assertThat(bitSet.get(i)).isFalse(); + } + + bitSet.set(10, 20); + bitSet.clear(); + for (int i = 0; i < 100; i++) { + assertThat(bitSet.get(i)).isFalse(); + } + } + + @Test + public void givenBitSet_whenGettingElements_thenShouldReturnRequestedBits() { + BitSet bitSet = new BitSet(); + bitSet.set(42); + + assertThat(bitSet.get(42)).isTrue(); + assertThat(bitSet.get(43)).isFalse(); + + bitSet.set(10, 20); + BitSet newBitSet = bitSet.get(10, 20); + for (int i = 0; i < 10; i++) { + assertThat(newBitSet.get(i)).isTrue(); + } + } + + @Test + public void givenBitSet_whenFlip_thenTogglesTrueToFalseAndViceVersa() { + BitSet bitSet = new BitSet(); + bitSet.set(42); + bitSet.flip(42); + assertThat(bitSet.get(42)).isFalse(); + + bitSet.flip(12); + assertThat(bitSet.get(12)).isTrue(); + + bitSet.flip(30, 40); + for (int i = 30; i < 40; i++) { + assertThat(bitSet.get(i)).isTrue(); + } + } + + @Test + public void givenBitSet_whenGettingTheSize_thenReturnsTheSize() { + BitSet defaultBitSet = new BitSet(); + assertThat(defaultBitSet.size()).isEqualTo(64); + + BitSet bitSet = new BitSet(1024); + assertThat(bitSet.size()).isEqualTo(1024); + + assertThat(bitSet.cardinality()).isEqualTo(0); + bitSet.set(10, 30); + assertThat(bitSet.cardinality()).isEqualTo(30 - 10); + + assertThat(bitSet.length()).isEqualTo(30); + bitSet.set(100); + assertThat(bitSet.length()).isEqualTo(101); + + assertThat(bitSet.isEmpty()).isFalse(); + bitSet.clear(); + assertThat(bitSet.isEmpty()).isTrue(); + } + + @Test + public void givenBitSet_whenSetOperations_thenShouldReturnAnotherBitSet() { + BitSet first = new BitSet(); + first.set(5, 10); + + BitSet second = new BitSet(); + second.set(7, 15); + + assertThat(first.intersects(second)).isTrue(); + + first.and(second); + assertThat(first.get(7)).isTrue(); + assertThat(first.get(8)).isTrue(); + assertThat(first.get(9)).isTrue(); + assertThat(first.get(10)).isFalse(); + + first.clear(); + first.set(5, 10); + + first.xor(second); + for (int i = 5; i < 7; i++) { + assertThat(first.get(i)).isTrue(); + } + for (int i = 10; i < 15; i++) { + assertThat(first.get(i)).isTrue(); + } + } + + @Test + public void givenBitSet_whenStream_thenStreamsAllSetBits() { + BitSet bitSet = new BitSet(); + bitSet.set(15, 25); + + bitSet.stream().forEach(bit -> LOG.debug(String.valueOf(bit))); + assertThat(bitSet.stream().count()).isEqualTo(10); + } + + @Test + public void givenBitSet_whenNextOrPrev_thenReturnsTheNextOrPrevClearOrSetBit() { + BitSet bitSet = new BitSet(); + bitSet.set(15, 25); + + assertThat(bitSet.nextSetBit(13)).isEqualTo(15); + assertThat(bitSet.nextSetBit(25)).isEqualTo(-1); + + assertThat(bitSet.nextClearBit(23)).isEqualTo(25); + + assertThat(bitSet.previousClearBit(24)).isEqualTo(14); + assertThat(bitSet.previousSetBit(29)).isEqualTo(24); + assertThat(bitSet.previousSetBit(14)).isEqualTo(-1); + } +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/clearvsremoveall/ClearVsRemoveAllUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/clearvsremoveall/ClearVsRemoveAllUnitTest.java new file mode 100644 index 0000000000..8056df13fd --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/clearvsremoveall/ClearVsRemoveAllUnitTest.java @@ -0,0 +1,40 @@ +package com.ossez.collections.clearvsremoveall; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll() + */ +class ClearVsRemoveAllUnitTest { + + /* + * Tests + */ + @Test + void givenArrayListWithElements_whenClear_thenListBecomesEmpty() { + Collection collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + + collection.clear(); + + assertTrue(collection.isEmpty()); + } + + @Test + void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() { + Collection firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); + Collection secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7)); + + firstCollection.removeAll(secondCollection); + + assertEquals(Arrays.asList(1, 2), firstCollection); + assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection); + } + +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashmapvshashtable/HashmapVsHashtableDifferenceUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashmapvshashtable/HashmapVsHashtableDifferenceUnitTest.java new file mode 100644 index 0000000000..796bd6422a --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashmapvshashtable/HashmapVsHashtableDifferenceUnitTest.java @@ -0,0 +1,98 @@ +package com.ossez.collections.hashmapvshashtable; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; +import java.util.ConcurrentModificationException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class HashmapVsHashtableDifferenceUnitTest { + + // null values + @Test(expected = NullPointerException.class) + public void givenHashtable_whenAddNullKey_thenNullPointerExceptionThrown() { + Hashtable table = new Hashtable(); + table.put(null, "value"); + } + + @Test(expected = NullPointerException.class) + public void givenHashtable_whenAddNullValue_thenNullPointerExceptionThrown() { + Hashtable table = new Hashtable(); + table.put("key", null); + } + + @Test + public void givenHashmap_whenAddNullKeyAndValue_thenObjectAdded() { + HashMap map = new HashMap(); + map.put(null, "value"); + map.put("key1", null); + map.put("key2", null); + + assertEquals(3, map.size()); + } + + // fail-fast iterator + @Test(expected = ConcurrentModificationException.class) + public void givenHashmap_whenModifyUnderlyingCollection_thenConcurrentModificationExceptionThrown() { + HashMap map = new HashMap(); + map.put("key1", "value1"); + map.put("key2", "value2"); + map.put("key3", "value3"); + + Iterator iterator = map.keySet().iterator(); + while(iterator.hasNext()){ + iterator.next(); + map.put("key4", "value4"); + } + } + + @Test + public void givenHashtable_whenModifyUnderlyingCollection_thenItHasNoEffectOnIteratedCollection() { + Hashtable table = new Hashtable(); + table.put("key1", "value1"); + table.put("key2", "value2"); + + List keysSelected = Lists.newArrayList(); + Enumeration keys = table.keys(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + keysSelected.add(key); + + if (key.equals("key1")) { + table.put("key3", "value3"); + } + } + + assertEquals(2, keysSelected.size()); + } + + // synchronized map + @Test + public void givenHashmap_thenCreateSynchronizedMap() { + HashMap map = new HashMap(); + map.put("key1", "value1"); + map.put("key2", "value2"); + map.put("key3", "value3"); + + Set> set = map.entrySet(); + synchronized (map) { + Iterator> it = set.iterator(); + while(it.hasNext()) { + Map.Entry elem = (Map.Entry)it.next(); + } + } + + Map syncMap = Collections.synchronizedMap(map); + } +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashset/HashSetUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashset/HashSetUnitTest.java new file mode 100644 index 0000000000..5ce009b236 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/hashset/HashSetUnitTest.java @@ -0,0 +1,33 @@ +package com.ossez.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)); + } + +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/iterators/IteratorsUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/iterators/IteratorsUnitTest.java new file mode 100644 index 0000000000..a1cc43fff2 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/iterators/IteratorsUnitTest.java @@ -0,0 +1,40 @@ +package com.ossez.collections.iterators; + +import static com.ossez.collections.collections.iterators.Iterators.failFast1; +import static com.ossez.collections.collections.iterators.Iterators.failFast2; +import static com.ossez.collections.collections.iterators.Iterators.failSafe1; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.ConcurrentModificationException; + +import org.junit.Test; + +/** + * Source code https://github.com/eugenp/tutorials + * + * @author Santosh Thakur + */ + +public class IteratorsUnitTest { + + @Test + public void whenFailFast_ThenThrowsException() { + assertThatThrownBy(() -> { + failFast1(); + }).isInstanceOf(ConcurrentModificationException.class); + } + + @Test + public void whenFailFast_ThenThrowsExceptionInSecondIteration() { + assertThatThrownBy(() -> { + failFast2(); + }).isInstanceOf(ConcurrentModificationException.class); + } + + @Test + public void whenFailSafe_ThenDoesNotThrowException() { + assertThat(failSafe1()).isGreaterThanOrEqualTo(0); + } + +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/mapfirstpair/MapFirstPairUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/mapfirstpair/MapFirstPairUnitTest.java new file mode 100644 index 0000000000..b878b96405 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/mapfirstpair/MapFirstPairUnitTest.java @@ -0,0 +1,146 @@ +package com.ossez.collections.mapfirstpair; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +public class MapFirstPairUnitTest { + + private Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) { + return null; + } + + Iterator> iterator = map.entrySet() + .iterator(); + + if (iterator.hasNext()) { + return iterator.next(); + } + + return null; + } + + private Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) { + return null; + } + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + + private Map populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + return map; + } + + @Test + public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() { + Map hashMap = new HashMap<>(); + hashMap = populateMapValues(hashMap); + + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); + Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + assertNotEquals(pairInsertedFirst, actualValue); + } + + @Test + public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() { + Map hashMap = new HashMap<>(); + hashMap = populateMapValues(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); + Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + assertNotEquals(pairInsertedFirst, actualValue); + } + + @Test + public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { + Map linkedHashMap = new LinkedHashMap<>(); + linkedHashMap = populateMapValues(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { + Map linkedHashMap = new LinkedHashMap<>(); + linkedHashMap = populateMapValues(linkedHashMap); + + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { + Map hashMap = new HashMap<>(); + hashMap = populateMapValues(hashMap); + + hashMap.put(0, "D"); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { + Map hashMap = new HashMap<>(); + hashMap = populateMapValues(hashMap); + + hashMap.put(0, "D"); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { + Map linkedHashMap = new LinkedHashMap<>(); + linkedHashMap = populateMapValues(linkedHashMap); + + linkedHashMap.put(0, "D"); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { + Map linkedHashMap = new LinkedHashMap<>(); + linkedHashMap = populateMapValues(linkedHashMap); + + linkedHashMap.put(0, "D"); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } +} diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/stack/StackUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/stack/StackUnitTest.java new file mode 100644 index 0000000000..37f721bdc7 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/ossez/collections/stack/StackUnitTest.java @@ -0,0 +1,183 @@ +package com.ossez.collections.stack; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.ListIterator; +import java.util.Stack; +import java.util.stream.Collectors; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +public class StackUnitTest { + + @Test + public void whenStackIsCreated_thenItHasSizeZero() { + Stack intStack = new Stack<>(); + + assertEquals(0, intStack.size()); + } + + @Test + public void whenElementIsPushed_thenStackSizeIsIncreased() { + Stack intStack = new Stack<>(); + intStack.push(1); + + assertEquals(1, intStack.size()); + } + + @Test + public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() { + Stack intStack = new Stack<>(); + List intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + + boolean result = intStack.addAll(intList); + + assertTrue(result); + assertEquals(7, intList.size()); + } + + @Test + public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() { + Stack intStack = new Stack<>(); + intStack.push(5); + + Integer element = intStack.pop(); + + assertEquals(Integer.valueOf(5), element); + assertTrue(intStack.isEmpty()); + } + + @Test + public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() { + Stack intStack = new Stack<>(); + intStack.push(5); + + Integer element = intStack.peek(); + + assertEquals(Integer.valueOf(5), element); + assertEquals(1, intStack.search(5)); + assertEquals(1, intStack.size()); + } + + @Test + public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() { + Stack intStack = new Stack<>(); + intStack.push(5); + intStack.push(8); + + assertEquals(2, intStack.search(5)); + } + + @Test + public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() { + Stack intStack = new Stack<>(); + intStack.push(5); + + int indexOf = intStack.indexOf(5); + + assertEquals(0, indexOf); + } + + @Test + public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() { + Stack intStack = new Stack<>(); + intStack.push(5); + intStack.push(5); + intStack.push(5); + + int lastIndexOf = intStack.lastIndexOf(5); + + assertEquals(2, lastIndexOf); + } + + @Test + public void whenRemoveElementIsInvoked_thenElementIsRemoved() { + Stack intStack = new Stack<>(); + intStack.push(5); + intStack.push(5); + + intStack.removeElement(5); + + assertEquals(1, intStack.size()); + } + + @Test + public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() { + Stack intStack = new Stack<>(); + intStack.push(5); + intStack.push(7); + + intStack.removeElementAt(1); + + assertEquals(-1, intStack.search(7)); + } + + @Test + public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() { + Stack intStack = new Stack<>(); + intStack.push(5); + intStack.push(7); + + intStack.removeAllElements(); + + assertTrue(intStack.isEmpty()); + } + + @Test + public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() { + Stack intStack = new Stack<>(); + List intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + intStack.addAll(intList); + intStack.add(500); + + intStack.removeAll(intList); + + assertEquals(1, intStack.size()); + assertEquals(1, intStack.search(500)); + } + + @Test + public void whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() { + Stack intStack = new Stack<>(); + List intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + intStack.addAll(intList); + + intStack.removeIf(element -> element < 6); + + assertEquals(2, intStack.size()); + } + + @Test + public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() { + Stack intStack = new Stack<>(); + List intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + intStack.addAll(intList); + + ListIterator it = intStack.listIterator(); + + Stack result = new Stack<>(); + while(it.hasNext()) { + result.push(it.next()); + } + + assertThat(result, equalTo(intStack)); + } + + @Test + public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() { + Stack intStack = new Stack<>(); + List inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10); + intStack.addAll(inputIntList); + + List filtered = intStack + .stream() + .filter(element -> element <= 3) + .collect(Collectors.toList()); + + assertEquals(3, filtered.size()); + } +} diff --git a/core-java-modules/core-java-collections-3/src/test/resources/logback.xml b/core-java-modules/core-java-collections-3/src/test/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file