BAEL-4717: Added HashMap and LinkedList examples

This commit is contained in:
Daniel Strmecki 2020-11-16 16:02:18 +01:00
parent 796aefa996
commit cc1a2e9b15
4 changed files with 77 additions and 2 deletions

View File

@ -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<String> 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<String> list = new ArrayList<>(Arrays.asList("Daniel", "Marko"));
list.remove(1);
assertThat(list).hasSize(1);
assertThat(list).doesNotContain("Marko");
}
}

View File

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

View File

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

View File

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