|
18 | 18 |
|
19 | 19 | public class JoinSplitCollectionsUnitTest { |
20 | 20 |
|
21 | | - @Test |
22 | | - public void whenJoiningTwoArrays_thenJoined() { |
23 | | - String[] animals1 = new String[] { "Dog", "Cat" }; |
24 | | - String[] animals2 = new String[] { "Bird", "Cow" }; |
25 | | - String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)) |
26 | | - .toArray(String[]::new); |
27 | | - |
28 | | - assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); |
29 | | - } |
30 | | - |
31 | | - @Test |
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()) |
36 | | - .collect(Collectors.toList()); |
37 | | - |
38 | | - assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12))); |
39 | | - } |
40 | | - |
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 | | - |
49 | | - assertTrue(result.equals(Arrays.asList(7, 8, 9, 10))); |
50 | | - } |
51 | | - |
52 | | - @Test |
53 | | - public void whenConvertArrayToString_thenConverted() { |
54 | | - String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" }; |
55 | | - String result = Arrays.stream(colors) |
56 | | - .collect(Collectors.joining(", ")); |
57 | | - |
58 | | - assertEquals(result, "Red, Blue, Green, Yellow"); |
59 | | - } |
60 | | - |
61 | | - @Test |
62 | | - public void whenConvertCollectionToString_thenConverted() { |
63 | | - Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom"); |
64 | | - String result = directions.stream() |
65 | | - .collect(Collectors.joining(", ")); |
66 | | - |
67 | | - assertEquals(result, "Left, Right, Top, Bottom"); |
68 | | - } |
69 | | - |
70 | | - @Test |
71 | | - public void whenConvertMapToString_thenConverted() { |
72 | | - Map<Integer, String> users = new HashMap<>(); |
73 | | - users.put(1, "John Doe"); |
74 | | - users.put(2, "Paul Smith"); |
75 | | - users.put(3, "Susan Anderson"); |
76 | | - |
77 | | - String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()) |
78 | | - .collect(Collectors.joining(", ")); |
79 | | - |
80 | | - assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"); |
81 | | - } |
82 | | - |
83 | | - @Test |
84 | | - public void whenConvertNestedCollectionToString_thenConverted() { |
85 | | - Collection<List<String>> nested = new ArrayList<>(); |
86 | | - nested.add(Arrays.asList("Left", "Right", "Top", "Bottom")); |
87 | | - nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow")); |
88 | | - |
89 | | - String result = nested.stream() |
90 | | - .map(nextList -> nextList.stream() |
91 | | - .collect(Collectors.joining("-"))) |
92 | | - .collect(Collectors.joining("; ")); |
93 | | - |
94 | | - assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow"); |
95 | | - } |
96 | | - |
97 | | - @Test |
98 | | - public void whenConvertCollectionToStringAndSkipNull_thenConverted() { |
99 | | - Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape"); |
100 | | - String result = fruits.stream() |
101 | | - .filter(next -> next != null) |
102 | | - .collect(Collectors.joining(", ")); |
103 | | - |
104 | | - assertEquals(result, "Apple, Orange, Grape"); |
105 | | - } |
106 | | - |
107 | | - @Test |
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 | | - }); |
123 | | - |
124 | | - assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5))); |
125 | | - assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10))); |
126 | | - } |
127 | | - |
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 | | - } |
138 | | - |
139 | | - @Test |
140 | | - public void whenConvertStringToArray_thenConverted() { |
141 | | - String colors = "Red, Blue, Green, Yellow"; |
142 | | - String[] result = colors.split(", "); |
143 | | - |
144 | | - assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" }); |
145 | | - } |
146 | | - |
147 | | - @Test |
148 | | - public void whenConvertStringToCollection_thenConverted() { |
149 | | - String colors = "Left, Right, Top, Bottom"; |
150 | | - Collection<String> result = Arrays.asList(colors.split(", ")); |
151 | | - |
152 | | - assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom"))); |
153 | | - } |
154 | | - |
155 | | - @Test |
156 | | - public void whenConvertStringToMap_thenConverted() { |
157 | | - String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"; |
158 | | - |
159 | | - Map<Integer, String> result = Arrays.stream(users.split(", ")) |
160 | | - .map(next -> next.split(" = ")) |
161 | | - .collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); |
162 | | - |
163 | | - assertEquals(result.get(1), "John Doe"); |
164 | | - assertEquals(result.get(2), "Paul Smith"); |
165 | | - assertEquals(result.get(3), "Susan Anderson"); |
166 | | - } |
167 | | - |
168 | | - @Test |
169 | | - public void whenConvertCollectionToStringMultipleSeparators_thenConverted() { |
170 | | - String fruits = "Apple. , Orange, Grape. Lemon"; |
171 | | - |
172 | | - Collection<String> result = Arrays.stream(fruits.split("[,|.]")) |
173 | | - .map(String::trim) |
174 | | - .filter(next -> !next.isEmpty()) |
175 | | - .collect(Collectors.toList()); |
176 | | - |
177 | | - assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon"))); |
178 | | - } |
| 21 | + @Test |
| 22 | + public void whenJoiningTwoArrays_thenJoined() { |
| 23 | + String[] animals1 = new String[] { "Dog", "Cat" }; |
| 24 | + String[] animals2 = new String[] { "Bird", "Cow" }; |
| 25 | + String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)) |
| 26 | + .toArray(String[]::new); |
| 27 | + |
| 28 | + assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 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()) |
| 36 | + .collect(Collectors.toList()); |
| 37 | + |
| 38 | + assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12))); |
| 39 | + } |
| 40 | + |
| 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 | + |
| 49 | + assertTrue(result.equals(Arrays.asList(7, 8, 9, 10))); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void whenConvertArrayToString_thenConverted() { |
| 54 | + String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" }; |
| 55 | + String result = Arrays.stream(colors) |
| 56 | + .collect(Collectors.joining(", ")); |
| 57 | + |
| 58 | + assertEquals(result, "Red, Blue, Green, Yellow"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenConvertCollectionToString_thenConverted() { |
| 63 | + Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom"); |
| 64 | + String result = directions.stream() |
| 65 | + .collect(Collectors.joining(", ")); |
| 66 | + |
| 67 | + assertEquals(result, "Left, Right, Top, Bottom"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenConvertMapToString_thenConverted() { |
| 72 | + Map<Integer, String> users = new HashMap<>(); |
| 73 | + users.put(1, "John Doe"); |
| 74 | + users.put(2, "Paul Smith"); |
| 75 | + users.put(3, "Susan Anderson"); |
| 76 | + |
| 77 | + String result = users.entrySet().stream() |
| 78 | + .map(entry -> entry.getKey() + " = " + entry.getValue()) |
| 79 | + .collect(Collectors.joining(", ")); |
| 80 | + |
| 81 | + assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void whenConvertNestedCollectionToString_thenConverted() { |
| 86 | + Collection<List<String>> nested = new ArrayList<>(); |
| 87 | + nested.add(Arrays.asList("Left", "Right", "Top", "Bottom")); |
| 88 | + nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow")); |
| 89 | + |
| 90 | + String result = nested.stream() |
| 91 | + .map(nextList -> nextList.stream() |
| 92 | + .collect(Collectors.joining("-"))) |
| 93 | + .collect(Collectors.joining("; ")); |
| 94 | + |
| 95 | + assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void whenConvertCollectionToStringAndSkipNull_thenConverted() { |
| 100 | + Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape"); |
| 101 | + String result = fruits.stream() |
| 102 | + .filter(next -> next != null) |
| 103 | + .collect(Collectors.joining(", ")); |
| 104 | + |
| 105 | + assertEquals(result, "Apple, Orange, Grape"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void whenSplitCollectionHalf_thenConverted() { |
| 110 | + Collection<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 111 | + Collection<Integer> result1 = new ArrayList<>(); |
| 112 | + Collection<Integer> result2 = new ArrayList<>(); |
| 113 | + AtomicInteger count = new AtomicInteger(); |
| 114 | + int midpoint = Math.round(numbers.size() / 2); |
| 115 | + |
| 116 | + numbers.forEach(next -> { |
| 117 | + int index = count.getAndIncrement(); |
| 118 | + if (index < midpoint) { |
| 119 | + result1.add(next); |
| 120 | + } else { |
| 121 | + result2.add(next); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5))); |
| 126 | + assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10))); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void whenSplitArrayByWordLength_thenConverted() { |
| 131 | + String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" }; |
| 132 | + Map<Integer, List<String>> result = Arrays.stream(words) |
| 133 | + .collect(Collectors.groupingBy(word -> word.length())); |
| 134 | + |
| 135 | + assertTrue(result.get(2).equals(Arrays.asList("it", "my"))); |
| 136 | + assertTrue(result.get(3).equals(Arrays.asList("bye", "and"))); |
| 137 | + assertTrue(result.get(4).equals(Arrays.asList("cold", "word"))); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void whenConvertStringToArray_thenConverted() { |
| 142 | + String colors = "Red, Blue, Green, Yellow"; |
| 143 | + String[] result = colors.split(", "); |
| 144 | + |
| 145 | + assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" }); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void whenConvertStringToCollection_thenConverted() { |
| 150 | + String colors = "Left, Right, Top, Bottom"; |
| 151 | + Collection<String> result = Arrays.asList(colors.split(", ")); |
| 152 | + |
| 153 | + assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom"))); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void whenConvertStringToMap_thenConverted() { |
| 158 | + String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"; |
| 159 | + |
| 160 | + Map<Integer, String> result = Arrays.stream(users.split(", ")) |
| 161 | + .map(next -> next.split(" = ")) |
| 162 | + .collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); |
| 163 | + |
| 164 | + assertEquals(result.get(1), "John Doe"); |
| 165 | + assertEquals(result.get(2), "Paul Smith"); |
| 166 | + assertEquals(result.get(3), "Susan Anderson"); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void whenConvertCollectionToStringMultipleSeparators_thenConverted() { |
| 171 | + String fruits = "Apple. , Orange, Grape. Lemon"; |
| 172 | + |
| 173 | + Collection<String> result = Arrays.stream(fruits.split("[,|.]")) |
| 174 | + .map(String::trim) |
| 175 | + .filter(next -> !next.isEmpty()) |
| 176 | + .collect(Collectors.toList()); |
| 177 | + |
| 178 | + assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon"))); |
| 179 | + } |
179 | 180 | } |
0 commit comments