File tree Expand file tree Collapse file tree 7 files changed +173
-0
lines changed
src/test/java/com/baeldung/collections/comparation Expand file tree Collapse file tree 7 files changed +173
-0
lines changed Original file line number Diff line number Diff line change 1+ =========
2+
3+ ## Core Java Collections Cookbooks and Examples
4+
5+ ### Relevant Articles:
6+
7+ - TODO: add article links here
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project
3+ xmlns =" http://maven.apache.org/POM/4.0.0"
4+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
5+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
6+ <modelVersion >4.0.0</modelVersion >
7+ <artifactId >core-java-collections-4</artifactId >
8+ <version >0.1.0-SNAPSHOT</version >
9+ <name >core-java-collections-4</name >
10+ <packaging >jar</packaging >
11+ <parent >
12+ <groupId >com.baeldung.core-java-modules</groupId >
13+ <artifactId >core-java-modules</artifactId >
14+ <version >0.0.1-SNAPSHOT</version >
15+ <relativePath >../pom.xml</relativePath >
16+ </parent >
17+
18+ <dependencies >
19+ <dependency >
20+ <groupId >org.assertj</groupId >
21+ <artifactId >assertj-core</artifactId >
22+ <version >${assertj.version} </version >
23+ <scope >test</scope >
24+ </dependency >
25+ </dependencies >
26+
27+ <properties >
28+ <assertj .version>3.18.0</assertj .version>
29+ </properties >
30+
31+ </project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .collections .comparation ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .Arrays ;
7+ import java .util .List ;
8+
9+ import static org .assertj .core .api .Assertions .assertThat ;
10+
11+ public class ArrayListUnitTest {
12+
13+ @ Test
14+ void givenArrayList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved () {
15+ List <String > list = new ArrayList <>();
16+ list .add ("Daniel" );
17+ list .add (0 , "Marko" );
18+ assertThat (list ).hasSize (2 );
19+ assertThat (list .get (0 )).isEqualTo ("Marko" );
20+ }
21+
22+ @ Test
23+ void givenArrayList_whenItemRemovedViaIndex_thenListSizeIsReduced () {
24+ List <String > list = new ArrayList <>(Arrays .asList ("Daniel" , "Marko" ));
25+ list .remove (1 );
26+ assertThat (list ).hasSize (1 );
27+ assertThat (list ).doesNotContain ("Marko" );
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .collections .comparation ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .Arrays ;
6+ import java .util .HashMap ;
7+ import java .util .LinkedList ;
8+ import java .util .Map ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+
12+ public class HashMapUnitTest {
13+
14+ @ Test
15+ void givenHashMap_whenItemAddedByKey_thenItCanBeRetrieved () {
16+ Map <String , String > map = new HashMap <>();
17+ map .put ("123456" , "Daniel" );
18+ map .put ("654321" , "Marko" );
19+ assertThat (map .get ("654321" )).isEqualTo ("Marko" );
20+ }
21+
22+ @ Test
23+ void givenHashMap_whenItemRemovedByKey_thenMapSizeIsReduced () {
24+ Map <String , String > map = new HashMap <>();
25+ map .put ("123456" , "Daniel" );
26+ map .put ("654321" , "Marko" );
27+ map .remove ("654321" );
28+ assertThat (map ).hasSize (1 );
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .collections .comparation ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .Arrays ;
7+ import java .util .LinkedList ;
8+ import java .util .List ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+
12+ public class LinkedListUnitTest {
13+
14+ @ Test
15+ void givenLinkedList_whenItemIsAppended_thenItCanBeRetrieved () {
16+ LinkedList <String > list = new LinkedList <>();
17+ list .addLast ("Daniel" );
18+ list .addFirst ("Marko" );
19+ assertThat (list ).hasSize (2 );
20+ assertThat (list .getLast ()).isEqualTo ("Daniel" );
21+ }
22+
23+ @ Test
24+ void givenLinkedList_whenItemIsRemoved_thenListSizeIsReduced () {
25+ LinkedList <String > list = new LinkedList <>(Arrays .asList ("Daniel" , "Marko" , "David" ));
26+ list .removeFirst ();
27+ list .removeLast ();
28+ assertThat (list ).hasSize (1 );
29+ assertThat (list ).containsExactly ("Marko" );
30+ }
31+
32+ @ Test
33+ void givenLinkedList_whenItemInserted_thenItCanBeRetrievedAndDeleted () {
34+ LinkedList <String > list = new LinkedList <>();
35+ list .push ("Daniel" );
36+ list .push ("Marko" );
37+ assertThat (list .poll ()).isEqualTo ("Marko" );
38+ assertThat (list ).hasSize (1 );
39+ }
40+
41+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .collections .comparation ;
2+
3+ import static org .assertj .core .api .Assertions .*;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .util .*;
7+
8+ class ListVsMapUnitTest {
9+
10+ @ Test
11+ void givenList_whenIteratingTroughValues_thenEachValueIsPresent () {
12+ List <String > list = new ArrayList <>();
13+ list .add ("Daniel" );
14+ list .add ("Marko" );
15+ for (String name : list ) {
16+ assertThat (name ).isIn (list );
17+ }
18+ assertThat (list ).containsExactly ("Daniel" , "Marko" );
19+ }
20+
21+ @ Test
22+ void givenMap_whenIteratingTroughValues_thenEachValueIsPresent () {
23+ Map <Integer , String > map = new HashMap <>();
24+ map .put (1 , "Daniel" );
25+ map .put (2 , "Marko" );
26+ for (String name : map .values ()) {
27+ assertThat (name ).isIn (map .values ());
28+ }
29+ assertThat (map .values ()).containsExactlyInAnyOrder ("Daniel" , "Marko" );
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 3434 <module >core-java-collections</module >
3535 <module >core-java-collections-2</module >
3636 <module >core-java-collections-3</module >
37+ <module >core-java-collections-4</module >
3738 <module >core-java-collections-array-list</module >
3839 <module >core-java-collections-list</module >
3940 <module >core-java-collections-list-2</module >
You can’t perform that action at this time.
0 commit comments