|
| 1 | +package org.baeldung.java.collections; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class JoinSplitCollectionsUnitTest { |
| 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 | + } |
| 179 | +} |
0 commit comments