@@ -22,12 +22,15 @@ public void declare() {
2222 }
2323
2424 @ Test
25- public void initInOneLine () {
26- LinkedList <Integer > linkedList1 = new LinkedList <>(Arrays . asList ( 1 , 2 , 3 ));
27- assertThat (linkedList1 ).hasSize ( 3 );
25+ public void initWithFactoryMethodsInOneLine () {
26+ LinkedList <Integer > linkedList1 = new LinkedList <>(List . of ( 3 , 1 , 2 , 2 ));
27+ assertThat (linkedList1 ).containsExactly ( 3 , 1 , 2 , 2 );
2828
29- LinkedList <Integer > linkedList2 = new LinkedList <>(List .of (1 , 2 , 3 ));
30- assertThat (linkedList2 ).hasSize (3 );
29+ LinkedList <Integer > linkedList2 = new LinkedList <>(Set .of (1 , 2 , 3 ));
30+ assertThat (linkedList2 ).contains (1 , 2 , 3 );
31+
32+ LinkedList <Integer > linkedList3 = new LinkedList <>(Arrays .asList (3 , 1 , 2 , 2 , null ));
33+ assertThat (linkedList3 ).containsExactly (3 , 1 , 2 , 2 , null );
3134 }
3235
3336 @ Test
@@ -46,9 +49,12 @@ public void initWithAddAll() {
4649 LinkedList <Integer > linkedList = new LinkedList <>();
4750 linkedList .add (1 );
4851 linkedList .add (2 );
52+ linkedList .add (2 ); // can add duplicated element
53+ linkedList .add (null ); // add null element
4954 linkedList .addAll (List .of (4 , 5 , 6 ));
5055
51- assertThat (linkedList ).hasSize (5 );
56+ // The iteration order is same as the insertion order
57+ assertThat (linkedList ).containsExactly (1 , 2 , 2 , null , 4 , 5 , 6 );
5258 }
5359
5460 @ Test
@@ -58,13 +64,19 @@ public void iterateWithForEachConsumer() {
5864 }
5965
6066 @ Test
61- public void iterateWithIterator () {
67+ public void iterateWithIteratorAndListIterator () {
6268 LinkedList <Integer > linkedList = new LinkedList <>(List .of (1 , 2 , 3 ));
6369
70+ // Iterate forward with Iterator
6471 Iterator <Integer > iterator = linkedList .iterator ();
6572 while (iterator .hasNext ()){
6673 System .out .printf ("%d " , iterator .next ());
6774 }
75+
76+ ListIterator <Integer > listIterator = linkedList .listIterator (linkedList .size ());
77+ while (listIterator .hasPrevious ()){
78+ System .out .printf ("%d " , listIterator .previous ());
79+ }
6880 }
6981
7082 @ Test
@@ -79,24 +91,124 @@ public void retrieving() {
7991 }
8092
8193 @ Test
82- public void adding () {
94+ public void filterAndRetrieve () {
95+ LinkedList <Integer > linkedList = new LinkedList <>(List .of (1 , 2 , 3 ));
96+
97+ Integer firstElement = linkedList .stream ().findFirst ().orElse (null );
98+ assertThat (firstElement ).isEqualTo (1 );
99+
100+ Integer [] arr = linkedList .stream ().filter (e -> e >= 2 ).toArray (Integer []::new );
101+ assertThat (arr ).contains (2 , 3 );
102+ }
103+
104+ @ Test
105+ public void addAndRemove () {
83106 LinkedList <Integer > linkedList = new LinkedList <>(List .of (1 , 2 , 3 ));
84107
85108 linkedList .addFirst (0 );
86109 assertThat (linkedList ).containsExactly (0 , 1 , 2 , 3 );
87110
88111 linkedList .addLast (4 );
89112 assertThat (linkedList ).containsExactly (0 , 1 , 2 , 3 , 4 );
113+
114+ linkedList .removeFirst ();
115+ assertThat (linkedList ).containsExactly (1 , 2 , 3 , 4 );
116+
117+ linkedList .removeLast ();
118+ assertThat (linkedList ).containsExactly (1 , 2 , 3 );
90119 }
91120
92121 @ Test
93- public void removing () {
122+ public void examineAndUpdate () {
94123 LinkedList <Integer > linkedList = new LinkedList <>(List .of (1 , 2 , 3 ));
95124
96- linkedList .removeFirst ( );
97- assertThat (linkedList ). containsExactly ( 2 , 3 );
125+ boolean contained = linkedList .contains ( 1 );
126+ assertThat (contained ). isTrue ( );
98127
99- linkedList .removeLast ();
100- assertThat (linkedList ).containsExactly (2 );
128+ linkedList .set (1 , 20 );
129+ assertThat (linkedList ).containsExactly (1 , 20 , 3 );
130+ }
131+
132+ @ Test
133+ public void objectsComparingProblem (){
134+ LinkedList <Book > linkedList = new LinkedList <>();
135+ linkedList .add (new Book (1 , "a" ));
136+
137+ Boolean contained = linkedList .contains (new Book (1 , "a" ));
138+ assertThat (contained ).isFalse ();
139+
140+ linkedList .remove (new Book (1 , "a" ));
141+ assertThat (linkedList ).hasSize (1 );
142+ }
143+
144+ static class Book {
145+ int id ;
146+ String title ;
147+
148+ Book (int id , String title ) {
149+ this .id = id ;
150+ this .title = title ;
151+ }
152+
153+ int getId () {
154+ return id ;
155+ }
156+
157+ String getTitle () {
158+ return title ;
159+ }
160+ }
161+
162+ @ Test
163+ public void objectsComparingFixed (){
164+ LinkedList <BookFixed > linkedList = new LinkedList <>();
165+ linkedList .add (new BookFixed (1 , "a" ));
166+
167+ Boolean contained = linkedList .contains (new BookFixed (1 , "a" ));
168+ assertThat (contained ).isTrue ();
169+
170+ linkedList .remove (new BookFixed (1 , "a" ));
171+ assertThat (linkedList ).hasSize (0 );
172+ }
173+
174+ static class BookFixed {
175+ int id ;
176+ String title ;
177+
178+ BookFixed (int id , String title ) {
179+ this .id = id ;
180+ this .title = title ;
181+ }
182+
183+ int getId () {
184+ return id ;
185+ }
186+
187+ String getTitle () {
188+ return title ;
189+ }
190+
191+ @ Override
192+ public boolean equals (Object o ) {
193+ if (this == o ) return true ;
194+ if (o == null || getClass () != o .getClass ()) return false ;
195+ BookFixed bookFixed = (BookFixed ) o ;
196+ return id == bookFixed .id &&
197+ Objects .equals (title , bookFixed .title );
198+ }
199+ }
200+
201+ @ Test
202+ public void sortMultipleFields () {
203+ Book book1 = new Book (1 , "b" );
204+ Book book2 = new Book (2 , "c" );
205+ Book book3 = new Book (3 , "c" );
206+
207+ LinkedList <Book > list = new LinkedList <>(Arrays .asList (book1 , book2 , book3 ));
208+ list .sort (Comparator
209+ .comparing (Book ::getTitle , Comparator .reverseOrder ())
210+ .thenComparing (Book ::getId , Comparator .reverseOrder ()));
211+
212+ assertThat (list ).containsExactly (book3 , book2 , book1 );
101213 }
102214}
0 commit comments