Merge pull request #10258 from dstr89/feature/BAEL-4717-hashmap-linkedlist-arraylist
Feature/bael 4717 hashmap linkedlist arraylist
This commit is contained in:
commit
da8a8f7fa2
7
core-java-modules/core-java-collections-4/README.md
Normal file
7
core-java-modules/core-java-collections-4/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java Collections Cookbooks and Examples
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
|
||||||
|
- TODO: add article links here
|
31
core-java-modules/core-java-collections-4/pom.xml
Normal file
31
core-java-modules/core-java-collections-4/pom.xml
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?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-4</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-collections-4</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<assertj.version>3.18.0</assertj.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,30 @@
|
|||||||
|
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 givenArrayList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved() {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
list.add("Daniel");
|
||||||
|
list.add(0, "Marko");
|
||||||
|
assertThat(list).hasSize(2);
|
||||||
|
assertThat(list.get(0)).isEqualTo("Marko");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenArrayList_whenItemRemovedViaIndex_thenListSizeIsReduced() {
|
||||||
|
List<String> list = new ArrayList<>(Arrays.asList("Daniel", "Marko"));
|
||||||
|
list.remove(1);
|
||||||
|
assertThat(list).hasSize(1);
|
||||||
|
assertThat(list).doesNotContain("Marko");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String, String> map = new HashMap<>();
|
||||||
|
map.put("123456", "Daniel");
|
||||||
|
map.put("654321", "Marko");
|
||||||
|
assertThat(map.get("654321")).isEqualTo("Marko");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenHashMap_whenItemRemovedByKey_thenMapSizeIsReduced() {
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
map.put("123456", "Daniel");
|
||||||
|
map.put("654321", "Marko");
|
||||||
|
map.remove("654321");
|
||||||
|
assertThat(map).hasSize(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String> list = new LinkedList<>();
|
||||||
|
list.addLast("Daniel");
|
||||||
|
list.addFirst("Marko");
|
||||||
|
assertThat(list).hasSize(2);
|
||||||
|
assertThat(list.getLast()).isEqualTo("Daniel");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLinkedList_whenItemIsRemoved_thenListSizeIsReduced() {
|
||||||
|
LinkedList<String> 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<String> list = new LinkedList<>();
|
||||||
|
list.push("Daniel");
|
||||||
|
list.push("Marko");
|
||||||
|
assertThat(list.poll()).isEqualTo("Marko");
|
||||||
|
assertThat(list).hasSize(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
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<String> list = new ArrayList<>();
|
||||||
|
list.add("Daniel");
|
||||||
|
list.add("Marko");
|
||||||
|
for (String name : list) {
|
||||||
|
assertThat(name).isIn(list);
|
||||||
|
}
|
||||||
|
assertThat(list).containsExactly("Daniel", "Marko");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMap_whenIteratingTroughValues_thenEachValueIsPresent() {
|
||||||
|
Map<Integer, String> map = new HashMap<>();
|
||||||
|
map.put(1, "Daniel");
|
||||||
|
map.put(2, "Marko");
|
||||||
|
for (String name : map.values()) {
|
||||||
|
assertThat(name).isIn(map.values());
|
||||||
|
}
|
||||||
|
assertThat(map.values()).containsExactlyInAnyOrder("Daniel", "Marko");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -34,6 +34,7 @@
|
|||||||
<module>core-java-collections</module>
|
<module>core-java-collections</module>
|
||||||
<module>core-java-collections-2</module>
|
<module>core-java-collections-2</module>
|
||||||
<module>core-java-collections-3</module>
|
<module>core-java-collections-3</module>
|
||||||
|
<module>core-java-collections-4</module>
|
||||||
<module>core-java-collections-array-list</module>
|
<module>core-java-collections-array-list</module>
|
||||||
<module>core-java-collections-list</module>
|
<module>core-java-collections-list</module>
|
||||||
<module>core-java-collections-list-2</module>
|
<module>core-java-collections-list-2</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user