From 7077a5f80cf6faf74923a4bf6c2fc42aa4bc23b3 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 8 Nov 2020 11:15:42 +0100 Subject: [PATCH 1/6] BAEL-4717: Create new collections module --- .../core-java-collections-4/README.md | 7 +++++ .../core-java-collections-4/pom.xml | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 core-java-modules/core-java-collections-4/README.md create mode 100644 core-java-modules/core-java-collections-4/pom.xml diff --git a/core-java-modules/core-java-collections-4/README.md b/core-java-modules/core-java-collections-4/README.md new file mode 100644 index 0000000000..1c680b86ba --- /dev/null +++ b/core-java-modules/core-java-collections-4/README.md @@ -0,0 +1,7 @@ +========= + +## Core Java Collections Cookbooks and Examples + +### Relevant Articles: + +- TODO: add article links here diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml new file mode 100644 index 0000000000..23baa51d0d --- /dev/null +++ b/core-java-modules/core-java-collections-4/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + core-java-collections-3 + 0.1.0-SNAPSHOT + core-java-collections-3 + jar + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../pom.xml + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + 3.18.0 + + + From 796aefa9962896f00ede30cb94b7f4deb5a0b4cb Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 15 Nov 2020 11:47:08 +0100 Subject: [PATCH 2/6] BAEL-4717: Added ArrayList examples --- .../comparation/ArrayListUnitTest.java | 29 ++++++++++++++++++ .../comparation/ListVsMapUnitTest.java | 30 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java create mode 100644 core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java new file mode 100644 index 0000000000..935be19e74 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.collections.comparation; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ArrayListUnitTest { + + @Test + void givenList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved() { + List list = new ArrayList<>(); + list.add("Daniel"); + list.add(1, "Marko"); + assertThat(list).hasSize(2); + assertThat(list.get(1)).isEqualTo("Marko"); + } + + @Test + void givenList_whenItemRemovedViaIndex_thenListSizeIsReduced() { + List list = new ArrayList<>(Arrays.asList("Daniel", "Marko")); + list.remove(1); + assertThat(list).hasSize(1); + } + +} diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java new file mode 100644 index 0000000000..6507013fbf --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.collections.comparation; + +import static org.assertj.core.api.Assertions.*; +import org.junit.jupiter.api.Test; + +import java.util.*; + +class ListVsMapUnitTest { + + @Test + void givenList_whenIteratingTroughValues_thenEachValueIsPresent() { + List list = new ArrayList<>(); + list.add("Daniel"); + list.add("Marko"); + for (String name : list) { + assertThat(name).isIn(list); + } + } + + @Test + void givenMap_whenIteratingTroughValues_thenEachValueIsPresent() { + Map map = new HashMap<>(); + map.put(1, "Daniel"); + map.put(2, "Marko"); + for (String name : map.values()) { + assertThat(name).isIn(map.values()); + } + } + +} From cc1a2e9b15892f181240e867481335527dee8a00 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Mon, 16 Nov 2020 16:02:18 +0100 Subject: [PATCH 3/6] BAEL-4717: Added HashMap and LinkedList examples --- .../comparation/ArrayListUnitTest.java | 5 ++- .../comparation/HashMapUnitTest.java | 31 ++++++++++++++ .../comparation/LinkedListUnitTest.java | 41 +++++++++++++++++++ .../comparation/ListVsMapUnitTest.java | 2 + 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/HashMapUnitTest.java create mode 100644 core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java index 935be19e74..4041ff1637 100644 --- a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java @@ -11,7 +11,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class ArrayListUnitTest { @Test - void givenList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved() { + void givenArrayList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved() { List list = new ArrayList<>(); list.add("Daniel"); list.add(1, "Marko"); @@ -20,10 +20,11 @@ public class ArrayListUnitTest { } @Test - void givenList_whenItemRemovedViaIndex_thenListSizeIsReduced() { + void givenArrayList_whenItemRemovedViaIndex_thenListSizeIsReduced() { List list = new ArrayList<>(Arrays.asList("Daniel", "Marko")); list.remove(1); assertThat(list).hasSize(1); + assertThat(list).doesNotContain("Marko"); } } diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/HashMapUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/HashMapUnitTest.java new file mode 100644 index 0000000000..3b595472e0 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/HashMapUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.collections.comparation; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class HashMapUnitTest { + + @Test + void givenHashMap_whenItemAddedByKey_thenItCanBeRetrieved() { + Map map = new HashMap<>(); + map.put("123456", "Daniel"); + map.put("654321", "Marko"); + assertThat(map.get("654321")).isEqualTo("Marko"); + } + + @Test + void givenHashMap_whenItemRemovedByKey_thenMapSizeIsReduced() { + Map map = new HashMap<>(); + map.put("123456", "Daniel"); + map.put("654321", "Marko"); + map.remove("654321"); + assertThat(map).hasSize(1); + } + +} diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java new file mode 100644 index 0000000000..c2ab554c91 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.collections.comparation; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LinkedListUnitTest { + + @Test + void givenLinkedList_whenItemIsAppended_thenItCanBeRetrieved() { + LinkedList list = new LinkedList<>(); + list.addLast("Daniel"); + list.addFirst( "Marko"); + assertThat(list).hasSize(2); + assertThat(list.getLast()).isEqualTo("Daniel"); + } + + @Test + void givenLinkedList_whenItemIsRemoved_thenListSizeIsReduced() { + LinkedList list = new LinkedList<>(Arrays.asList("Daniel", "Marko", "David")); + list.removeFirst(); + list.removeLast(); + assertThat(list).hasSize(1); + assertThat(list).containsExactly("Marko"); + } + + @Test + void givenLinkedList_whenItemInserted_thenItCanBeRetrievedAndDeleted() { + LinkedList list = new LinkedList<>(); + list.push("Daniel"); + list.push("Marko"); + assertThat(list.poll()).isEqualTo("Marko"); + assertThat(list).hasSize(1); + } + +} diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java index 6507013fbf..dd6bf760fd 100644 --- a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ListVsMapUnitTest.java @@ -15,6 +15,7 @@ class ListVsMapUnitTest { for (String name : list) { assertThat(name).isIn(list); } + assertThat(list).containsExactly("Daniel", "Marko"); } @Test @@ -25,6 +26,7 @@ class ListVsMapUnitTest { for (String name : map.values()) { assertThat(name).isIn(map.values()); } + assertThat(map.values()).containsExactlyInAnyOrder("Daniel", "Marko"); } } From 79fc666e0709f947a2b0eaa33ffe49f95a05ffe3 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Tue, 17 Nov 2020 11:00:56 +0100 Subject: [PATCH 4/6] BAEL-4717: Refactor examples --- .../baeldung/collections/comparation/ArrayListUnitTest.java | 4 ++-- .../baeldung/collections/comparation/LinkedListUnitTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java index 4041ff1637..bc6a07d274 100644 --- a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/ArrayListUnitTest.java @@ -14,9 +14,9 @@ public class ArrayListUnitTest { void givenArrayList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved() { List list = new ArrayList<>(); list.add("Daniel"); - list.add(1, "Marko"); + list.add(0, "Marko"); assertThat(list).hasSize(2); - assertThat(list.get(1)).isEqualTo("Marko"); + assertThat(list.get(0)).isEqualTo("Marko"); } @Test diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java index c2ab554c91..aa6b7fa923 100644 --- a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/comparation/LinkedListUnitTest.java @@ -15,7 +15,7 @@ public class LinkedListUnitTest { void givenLinkedList_whenItemIsAppended_thenItCanBeRetrieved() { LinkedList list = new LinkedList<>(); list.addLast("Daniel"); - list.addFirst( "Marko"); + list.addFirst("Marko"); assertThat(list).hasSize(2); assertThat(list.getLast()).isEqualTo("Daniel"); } From c02a2052205d634961a4bf6d9c0d037ad46f62cf Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sat, 5 Dec 2020 09:52:32 +0100 Subject: [PATCH 5/6] BAEL-4717: add new module to parent pom --- core-java-modules/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index a6aecef741..a12b3548f9 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -33,6 +33,7 @@ core-java-collections core-java-collections-2 core-java-collections-3 + core-java-collections-4 core-java-collections-array-list core-java-collections-list core-java-collections-list-2 From 0d8d1d3a09978e35233ee5484306393f8d8dfb7b Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sun, 13 Dec 2020 10:33:53 +0100 Subject: [PATCH 6/6] BAEL-4717: Fix artifactId --- core-java-modules/core-java-collections-4/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml index 23baa51d0d..0e3cabf40e 100644 --- a/core-java-modules/core-java-collections-4/pom.xml +++ b/core-java-modules/core-java-collections-4/pom.xml @@ -4,9 +4,9 @@ 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"> 4.0.0 - core-java-collections-3 + core-java-collections-4 0.1.0-SNAPSHOT - core-java-collections-3 + core-java-collections-4 jar com.baeldung.core-java-modules