66
77import java .util .ArrayList ;
88import java .util .Arrays ;
9+ import java .util .Collection ;
910import java .util .HashMap ;
1011import java .util .List ;
1112import java .util .Map ;
13+ import java .util .concurrent .atomic .AtomicInteger ;
1214import java .util .stream .Collectors ;
1315import java .util .stream .Stream ;
1416
@@ -25,14 +27,25 @@ public void whenJoiningTwoArrays_thenJoined() {
2527
2628 assertArrayEquals (result , new String [] { "Dog" , "Cat" , "Bird" , "Cow" });
2729 }
28-
30+
2931 @ Test
30- public void whenJoiningTwoListsWithFilter_thenJoined () {
31- List <Integer > list1 = Arrays .asList (7 , 8 , 11 );
32- List <Integer > list2 = Arrays .asList (9 , 12 , 10 );
33- List <Integer > result = Stream .concat (list1 .stream (), list2 .stream ()). filter ( next -> next <= 10 )
32+ public void whenJoiningTwoCollections_thenJoined () {
33+ Collection <Integer > collection1 = Arrays .asList (7 , 8 , 9 );
34+ Collection <Integer > collection2 = Arrays .asList (10 , 11 , 12 );
35+ Collection <Integer > result = Stream .concat (collection1 .stream (), collection2 .stream ())
3436 .collect (Collectors .toList ());
37+
38+ assertTrue (result .equals (Arrays .asList (7 , 8 , 9 , 10 , 11 , 12 )));
39+ }
3540
41+ @ Test
42+ public void whenJoiningTwoCollectionsWithFilter_thenJoined () {
43+ Collection <Integer > collection1 = Arrays .asList (7 , 8 , 11 );
44+ Collection <Integer > collection2 = Arrays .asList (9 , 12 , 10 );
45+ Collection <Integer > result = Stream .concat (collection1 .stream (), collection2 .stream ())
46+ .filter (next -> next <= 10 )
47+ .collect (Collectors .toList ());
48+
3649 assertTrue (result .equals (Arrays .asList (7 , 8 , 9 , 10 )));
3750 }
3851
@@ -46,11 +59,11 @@ public void whenConvertArrayToString_thenConverted() {
4659 }
4760
4861 @ Test
49- public void whenConvertListToString_thenConverted () {
50- List <String > directions = Arrays .asList ("Left" , "Right" , "Top" , "Bottom" );
62+ public void whenConvertCollectionToString_thenConverted () {
63+ Collection <String > directions = Arrays .asList ("Left" , "Right" , "Top" , "Bottom" );
5164 String result = directions .stream ()
5265 .collect (Collectors .joining (", " ));
53-
66+
5467 assertEquals (result , "Left, Right, Top, Bottom" );
5568 }
5669
@@ -68,49 +81,60 @@ public void whenConvertMapToString_thenConverted() {
6881 }
6982
7083 @ Test
71- public void whenConvertNestedListToString_thenConverted () {
72- List <List <String >> nested = new ArrayList <>();
84+ public void whenConvertNestedCollectionToString_thenConverted () {
85+ Collection <List <String >> nested = new ArrayList <>();
7386 nested .add (Arrays .asList ("Left" , "Right" , "Top" , "Bottom" ));
7487 nested .add (Arrays .asList ("Red" , "Blue" , "Green" , "Yellow" ));
75-
88+
7689 String result = nested .stream ()
7790 .map (nextList -> nextList .stream ()
7891 .collect (Collectors .joining ("-" )))
7992 .collect (Collectors .joining ("; " ));
80-
93+
8194 assertEquals (result , "Left-Right-Top-Bottom; Red-Blue-Green-Yellow" );
8295 }
8396
8497 @ Test
85- public void whenConvertListToStringAndSkipNull_thenConverted () {
86- List <String > fruits = Arrays .asList ("Apple" , "Orange" , null , "Grape" );
98+ public void whenConvertCollectionToStringAndSkipNull_thenConverted () {
99+ Collection <String > fruits = Arrays .asList ("Apple" , "Orange" , null , "Grape" );
87100 String result = fruits .stream ()
88101 .filter (next -> next != null )
89102 .collect (Collectors .joining (", " ));
90-
103+
91104 assertEquals (result , "Apple, Orange, Grape" );
92105 }
93106
94107 @ Test
95- public void whenSplitListGroupOddEven_thenConverted () {
96- List <Integer > list = Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 );
97- Map <Boolean , List <Integer >> result = list .stream ()
98- .collect (Collectors .partitioningBy (number -> number % 2 == 0 ));
108+ public void whenSplitCollectionHalf_thenConverted () {
109+ Collection <Integer > numbers = Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 );
110+ Collection <Integer > result1 = new ArrayList <>();
111+ Collection <Integer > result2 = new ArrayList <>();
112+ AtomicInteger count = new AtomicInteger ();
113+ int midpoint = Math .round (numbers .size () / 2 );
114+
115+ numbers .forEach (next -> {
116+ int index = count .getAndIncrement ();
117+ if (index < midpoint ){
118+ result1 .add (next );
119+ }else {
120+ result2 .add (next );
121+ }
122+ });
99123
100- assertTrue (result . get ( true ). equals (Arrays .asList (2 , 4 , 6 , 8 , 10 )));
101- assertTrue (result . get ( false ). equals (Arrays .asList (1 , 3 , 5 , 7 , 9 )));
124+ assertTrue (result1 . equals (Arrays .asList (1 , 2 , 3 , 4 , 5 )));
125+ assertTrue (result2 . equals (Arrays .asList (6 , 7 , 8 , 9 , 10 )));
102126 }
103127
104- @ Test
105- public void whenSplitArrayByWordLength_thenConverted () {
106- String [] words = new String []{"bye" , "cold" , "it" , "and" , "my" , "word" };
107- Map <Integer , List <String >> result = Arrays .stream (words )
108- .collect (Collectors .groupingBy (word -> word .length ()));
109-
110- assertTrue (result .get (2 ).equals (Arrays .asList ("it" , "my" )));
111- assertTrue (result .get (3 ).equals (Arrays .asList ("bye" , "and" )));
112- assertTrue (result .get (4 ).equals (Arrays .asList ("cold" , "word" )));
113- }
128+ @ Test
129+ public void whenSplitArrayByWordLength_thenConverted () {
130+ String [] words = new String []{"bye" , "cold" , "it" , "and" , "my" , "word" };
131+ Map <Integer , List <String >> result = Arrays .stream (words )
132+ .collect (Collectors .groupingBy (word -> word .length ()));
133+
134+ assertTrue (result .get (2 ).equals (Arrays .asList ("it" , "my" )));
135+ assertTrue (result .get (3 ).equals (Arrays .asList ("bye" , "and" )));
136+ assertTrue (result .get (4 ).equals (Arrays .asList ("cold" , "word" )));
137+ }
114138
115139 @ Test
116140 public void whenConvertStringToArray_thenConverted () {
@@ -121,9 +145,9 @@ public void whenConvertStringToArray_thenConverted() {
121145 }
122146
123147 @ Test
124- public void whenConvertStringToList_thenConverted () {
148+ public void whenConvertStringToCollection_thenConverted () {
125149 String colors = "Left, Right, Top, Bottom" ;
126- List <String > result = Arrays .asList (colors .split (", " ));
150+ Collection <String > result = Arrays .asList (colors .split (", " ));
127151
128152 assertTrue (result .equals (Arrays .asList ("Left" , "Right" , "Top" , "Bottom" )));
129153 }
@@ -142,10 +166,10 @@ public void whenConvertStringToMap_thenConverted() {
142166 }
143167
144168 @ Test
145- public void whenConvertListToStringMultipleSeparators_thenConverted () {
169+ public void whenConvertCollectionToStringMultipleSeparators_thenConverted () {
146170 String fruits = "Apple. , Orange, Grape. Lemon" ;
147171
148- List <String > result = Arrays .stream (fruits .split ("[,|.]" ))
172+ Collection <String > result = Arrays .stream (fruits .split ("[,|.]" ))
149173 .map (String ::trim ).filter (next -> !next .isEmpty ())
150174 .collect (Collectors .toList ());
151175
0 commit comments