1
+ package com .examplehub .basics .collection ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+
5
+ import java .util .ArrayList ;
6
+ import java .util .Arrays ;
7
+ import java .util .Collection ;
8
+ import java .util .Iterator ;
9
+
10
+ import static org .junit .jupiter .api .Assertions .*;
11
+
12
+ class CollectionExampleTest {
13
+
14
+ @ Test
15
+ void testRemove () {
16
+ Collection <String > collection = new ArrayList <>();
17
+ collection .add ("1" );
18
+ collection .add ("2" );
19
+ collection .add ("3" );
20
+ collection .add ("4" );
21
+ assertTrue (collection .remove ("1" ));
22
+ assertTrue (collection .remove ("4" ));
23
+ assertFalse (collection .remove ("5" ));
24
+ assertEquals ("[2, 3]" , collection .toString ());
25
+ }
26
+
27
+ @ Test
28
+ void testRemoveAll () {
29
+ Collection <String > c1 = new ArrayList <>();
30
+ c1 .add ("1" );
31
+ c1 .add ("2" );
32
+ c1 .add ("3" );
33
+ c1 .add ("4" );
34
+
35
+ Collection <String > c2 = new ArrayList <>();
36
+ c2 .add ("2" );
37
+ c2 .add ("3" );
38
+ c2 .add ("5" );
39
+
40
+ assertTrue (c1 .removeAll (c2 ));
41
+ assertEquals ("[1, 4]" , c1 .toString ());
42
+ assertEquals ("[2, 3, 5]" , c2 .toString ());
43
+ }
44
+
45
+ @ Test
46
+ void testRetainAll () {
47
+ Collection <String > c1 = new ArrayList <>();
48
+ c1 .add ("1" );
49
+ c1 .add ("2" );
50
+ c1 .add ("3" );
51
+ c1 .add ("4" );
52
+
53
+ Collection <String > c2 = new ArrayList <>();
54
+ c2 .add ("2" );
55
+ c2 .add ("4" );
56
+ c2 .add ("5" );
57
+
58
+ assertTrue (c1 .retainAll (c2 ));
59
+ assertEquals ("[2, 4]" , c1 .toString ());
60
+ }
61
+
62
+ @ Test
63
+ void testToArray () {
64
+ Collection <String > collection = new ArrayList <>();
65
+ collection .add ("1" );
66
+ collection .add ("2" );
67
+ collection .add ("3" );
68
+ collection .add ("4" );
69
+ Object [] objects = collection .toArray ();
70
+ assertEquals ("[1, 2, 3, 4]" , Arrays .toString (objects ));
71
+ }
72
+
73
+ @ Test
74
+ void testConvertArrayToCollection () {
75
+ String [] digits = {"1" , "2" , "3" , "4" };
76
+ Collection <String > collection = Arrays .asList (digits );
77
+ assertEquals ("[1, 2, 3, 4]" , collection .toString ());
78
+ }
79
+
80
+ @ Test
81
+ void testIterator () {
82
+ Collection <String > collection = new ArrayList <>();
83
+ collection .add ("1" );
84
+ collection .add ("2" );
85
+ collection .add ("3" );
86
+ collection .add ("4" );
87
+
88
+ Iterator <String > iterator = collection .iterator ();
89
+ int count = 1 ;
90
+ while (iterator .hasNext ()) {
91
+ assertEquals (count ++ + "" , iterator .next ());
92
+ }
93
+ }
94
+
95
+ @ Test
96
+ void testWriteToString () {
97
+ Collection <String > collection = new ArrayList <>();
98
+ collection .add ("1" );
99
+ collection .add ("2" );
100
+ collection .add ("3" );
101
+ collection .add ("4" );
102
+
103
+ Iterator <String > iterator = collection .iterator ();
104
+ StringBuilder builder = new StringBuilder ();
105
+ if (!iterator .hasNext ()) {
106
+ builder .append ("[]" );
107
+ } else {
108
+ builder .append ("[" );
109
+ while (iterator .hasNext ()) {
110
+ String curItem = iterator .next ();
111
+ if (iterator .hasNext ()) {
112
+ builder .append (curItem ).append (", " );
113
+ } else {
114
+ builder .append (curItem ).append ("]" );
115
+ }
116
+ }
117
+ }
118
+ assertEquals ("[1, 2, 3, 4]" , builder .toString ());
119
+ }
120
+
121
+ @ Test
122
+ void testRemoveUsingIterator () {
123
+ Collection <String > collection = new ArrayList <>();
124
+ collection .add ("1" );
125
+ collection .add ("2" );
126
+ collection .add ("3" );
127
+ collection .add ("4" );
128
+
129
+ Iterator <String > iterator = collection .iterator ();
130
+ while (iterator .hasNext ()) {
131
+ iterator .next ();
132
+ iterator .remove ();
133
+ }
134
+ assertEquals ("[]" , collection .toString ());
135
+ }
136
+
137
+
138
+ @ Test
139
+ void testForEach () {
140
+ Collection <String > collection = new ArrayList <>();
141
+ collection .add ("1" );
142
+ collection .add ("2" );
143
+ collection .add ("3" );
144
+ collection .add ("4" );
145
+
146
+ int count = 1 ;
147
+ for (String item : collection ) {
148
+ assertEquals (count ++ + "" , item );
149
+ }
150
+ }
151
+ }
0 commit comments