core-java-collections-3 模块添加到仓库列表中

This commit is contained in:
YuCheng Hu 2022-05-03 11:59:45 -04:00
parent fc45db9033
commit 0d169bb516
19 changed files with 1349 additions and 0 deletions

View File

@ -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)

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.ossez.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>${jol-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>
<jol-core.version>0.10</jol-core.version>
</properties>
</project>

View File

@ -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<Employee> employeeList = new ArrayList<>();
Vector<Employee> employeeVector = new Vector<>();
//LinkedList<Employee> 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();
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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<String> 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<String> iterator = vector.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

View File

@ -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<Employee> employeeSet = new HashSet<>();
private List<Employee> 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();
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
Integer number = iterator.next();
numbers.add(50);
}
return numbers.size();
}
public static int failFast2() {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
Iterator<Integer> 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<String, Integer> map = new ConcurrentHashMap<>();
map.put("First", 10);
map.put("Second", 20);
map.put("Third", 30);
map.put("Fourth", 40);
Iterator<String> iterator = map.keySet()
.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
map.put("Fifth", 50);
}
return map.size();
}
}

View File

@ -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<Employee> employeeSet1 = new HashSet<>();
private List<Employee> employeeList1 = new ArrayList<>();
private Set<Employee> employeeSet2 = new HashSet<>();
private List<Employee> employeeList2 = new ArrayList<>();
private Set<Employee> employeeSet3 = new HashSet<>();
private Set<Employee> 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();
}
}

View File

@ -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();
}
}

View File

@ -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<Integer> 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));
}
}

View File

@ -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);
}
}

View File

@ -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<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
collection.clear();
assertTrue(collection.isEmpty());
}
@Test
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Collection<Integer> 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);
}
}

View File

@ -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<String, String> table = new Hashtable<String, String>();
table.put(null, "value");
}
@Test(expected = NullPointerException.class)
public void givenHashtable_whenAddNullValue_thenNullPointerExceptionThrown() {
Hashtable<String, String> table = new Hashtable<String, String>();
table.put("key", null);
}
@Test
public void givenHashmap_whenAddNullKeyAndValue_thenObjectAdded() {
HashMap<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
Iterator<String> iterator = map.keySet().iterator();
while(iterator.hasNext()){
iterator.next();
map.put("key4", "value4");
}
}
@Test
public void givenHashtable_whenModifyUnderlyingCollection_thenItHasNoEffectOnIteratedCollection() {
Hashtable<String, String> table = new Hashtable<String, String>();
table.put("key1", "value1");
table.put("key2", "value2");
List<String> keysSelected = Lists.newArrayList();
Enumeration<String> 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<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
Set<Entry<String, String>> set = map.entrySet();
synchronized (map) {
Iterator<Entry<String, String>> it = set.iterator();
while(it.hasNext()) {
Map.Entry<String, String> elem = (Map.Entry<String, String>)it.next();
}
}
Map<String, String> syncMap = Collections.synchronizedMap(map);
}
}

View File

@ -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<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));
}
}

View File

@ -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);
}
}

View File

@ -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<Integer, String> getFirstPairUsingIterator(Map<Integer, String> map) {
if (map == null || map.size() == 0) {
return null;
}
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet()
.iterator();
if (iterator.hasNext()) {
return iterator.next();
}
return null;
}
private Map.Entry<Integer, String> getFirstPairUsingStream(Map<Integer, String> map) {
if (map == null || map.size() == 0) {
return null;
}
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
return entrySet.stream()
.findFirst()
.get();
}
private Map<Integer, String> populateMapValues(Map<Integer, String> map) {
if (map != null) {
map.put(5, "A");
map.put(1, "B");
map.put(2, "C");
}
return map;
}
@Test
public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
Map<Integer, String> hashMap = new HashMap<>();
hashMap = populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
assertNotEquals(pairInsertedFirst, actualValue);
}
@Test
public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
Map<Integer, String> hashMap = new HashMap<>();
hashMap = populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
assertNotEquals(pairInsertedFirst, actualValue);
}
@Test
public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap = populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap = populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() {
Map<Integer, String> hashMap = new HashMap<>();
hashMap = populateMapValues(hashMap);
hashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() {
Map<Integer, String> hashMap = new HashMap<>();
hashMap = populateMapValues(hashMap);
hashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap = populateMapValues(linkedHashMap);
linkedHashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap = populateMapValues(linkedHashMap);
linkedHashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
}
}

View File

@ -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<Integer> intStack = new Stack<>();
assertEquals(0, intStack.size());
}
@Test
public void whenElementIsPushed_thenStackSizeIsIncreased() {
Stack<Integer> intStack = new Stack<>();
intStack.push(1);
assertEquals(1, intStack.size());
}
@Test
public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() {
Stack<Integer> intStack = new Stack<>();
List<Integer> 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<Integer> intStack = new Stack<>();
intStack.push(5);
Integer element = intStack.pop();
assertEquals(Integer.valueOf(5), element);
assertTrue(intStack.isEmpty());
}
@Test
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
Stack<Integer> 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<Integer> intStack = new Stack<>();
intStack.push(5);
intStack.push(8);
assertEquals(2, intStack.search(5));
}
@Test
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
Stack<Integer> intStack = new Stack<>();
intStack.push(5);
int indexOf = intStack.indexOf(5);
assertEquals(0, indexOf);
}
@Test
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
Stack<Integer> 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<Integer> intStack = new Stack<>();
intStack.push(5);
intStack.push(5);
intStack.removeElement(5);
assertEquals(1, intStack.size());
}
@Test
public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
Stack<Integer> intStack = new Stack<>();
intStack.push(5);
intStack.push(7);
intStack.removeElementAt(1);
assertEquals(-1, intStack.search(7));
}
@Test
public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
Stack<Integer> intStack = new Stack<>();
intStack.push(5);
intStack.push(7);
intStack.removeAllElements();
assertTrue(intStack.isEmpty());
}
@Test
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
Stack<Integer> intStack = new Stack<>();
List<Integer> 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<Integer> intStack = new Stack<>();
List<Integer> 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<Integer> intStack = new Stack<>();
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
intStack.addAll(intList);
ListIterator<Integer> it = intStack.listIterator();
Stack<Integer> result = new Stack<>();
while(it.hasNext()) {
result.push(it.next());
}
assertThat(result, equalTo(intStack));
}
@Test
public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() {
Stack<Integer> intStack = new Stack<>();
List<Integer> inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
intStack.addAll(inputIntList);
List<Integer> filtered = intStack
.stream()
.filter(element -> element <= 3)
.collect(Collectors.toList());
assertEquals(3, filtered.size());
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>