Skip to content

Commit 3db2e80

Browse files
committed
Unify, simply, format
1 parent 6038c51 commit 3db2e80

File tree

1 file changed

+75
-55
lines changed

1 file changed

+75
-55
lines changed

core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,85 +15,99 @@ public class JoinSplitCollectionsUnitTest {
1515
public void whenJoiningTwoArrays_thenJoined() {
1616
String[] animals1 = new String[] { "Dog", "Cat" };
1717
String[] animals2 = new String[] { "Bird", "Cow" };
18-
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
18+
String[] result = Stream.concat(
19+
Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
1920

2021
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
2122
}
2223

2324
@Test
2425
public void whenJoiningTwoCollections_thenJoined() {
25-
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
26-
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
27-
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList());
26+
Collection<String> collection1 = Arrays.asList("Dog", "Cat");
27+
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
28+
Collection<String> result = Stream.concat(
29+
collection1.stream(), collection2.stream())
30+
.collect(Collectors.toList());
2831

29-
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
32+
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose")));
3033
}
3134

3235
@Test
3336
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
34-
Collection<Integer> collection1 = Arrays.asList(7, 8, 11);
35-
Collection<Integer> collection2 = Arrays.asList(9, 12, 10);
36-
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).filter(next -> next <= 10).collect(Collectors.toList());
37-
38-
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10)));
37+
Collection<String> collection1 = Arrays.asList("Dog", "Cat");
38+
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
39+
Collection<String> result = Stream.concat(
40+
collection1.stream(), collection2.stream())
41+
.filter(e -> e.length() == 3)
42+
.collect(Collectors.toList());
43+
44+
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow")));
3945
}
4046

4147
@Test
4248
public void whenConvertArrayToString_thenConverted() {
43-
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" };
44-
String result = Arrays.stream(colors).collect(Collectors.joining(", "));
49+
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow" };
50+
String result = Arrays.stream(animals).collect(Collectors.joining(", "));
4551

46-
assertEquals(result, "Red, Blue, Green, Yellow");
52+
assertEquals(result, "Dog, Cat, Bird, Cow");
4753
}
4854

4955
@Test
5056
public void whenConvertCollectionToString_thenConverted() {
51-
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
52-
String result = directions.stream().collect(Collectors.joining(", "));
57+
Collection<String> animals = Arrays.asList("Dog", "Cat", "Bird", "Cow");
58+
String result = animals.stream().collect(Collectors.joining(", "));
5359

54-
assertEquals(result, "Left, Right, Top, Bottom");
60+
assertEquals(result, "Dog, Cat, Bird, Cow");
5561
}
5662

5763
@Test
5864
public void whenConvertMapToString_thenConverted() {
59-
Map<Integer, String> users = new HashMap<>();
60-
users.put(1, "John Doe");
61-
users.put(2, "Paul Smith");
62-
users.put(3, "Susan Anderson");
65+
Map<Integer, String> animals = new HashMap<>();
66+
animals.put(1, "Dog");
67+
animals.put(2, "Cat");
68+
animals.put(3, "Cow");
6369

64-
String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", "));
70+
String result = animals.entrySet().stream()
71+
.map(entry -> entry.getKey() + " = " + entry.getValue())
72+
.collect(Collectors.joining(", "));
6573

66-
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
74+
assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow");
6775
}
6876

6977
@Test
7078
public void whenConvertNestedCollectionToString_thenConverted() {
7179
Collection<List<String>> nested = new ArrayList<>();
72-
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
73-
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
80+
nested.add(Arrays.asList("Dog", "Cat"));
81+
nested.add(Arrays.asList("Cow", "Pig"));
7482

75-
String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; "));
83+
String result = nested.stream().map(
84+
nextList -> nextList.stream()
85+
.collect(Collectors.joining("-")))
86+
.collect(Collectors.joining("; "));
7687

77-
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
88+
assertEquals(result, "Dog-Cat; Cow-Pig");
7889
}
7990

8091
@Test
8192
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
82-
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
83-
String result = fruits.stream().filter(Objects::nonNull).collect(Collectors.joining(", "));
93+
Collection<String> animals = Arrays.asList("Dog", "Cat", null, "Moose");
94+
String result = animals.stream()
95+
.filter(Objects::nonNull)
96+
.collect(Collectors.joining(", "));
8497

85-
assertEquals(result, "Apple, Orange, Grape");
98+
assertEquals(result, "Dog, Cat, Moose");
8699
}
87100

88101
@Test
89102
public void whenSplitCollectionHalf_thenConverted() {
90-
Collection<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
91-
Collection<Integer> result1 = new ArrayList<>();
92-
Collection<Integer> result2 = new ArrayList<>();
103+
Collection<String> animals = Arrays.asList(
104+
"Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
105+
Collection<String> result1 = new ArrayList<>();
106+
Collection<String> result2 = new ArrayList<>();
93107
AtomicInteger count = new AtomicInteger();
94-
int midpoint = Math.round(numbers.size() / 2);
108+
int midpoint = Math.round(animals.size() / 2);
95109

96-
numbers.forEach(next -> {
110+
animals.forEach(next -> {
97111
int index = count.getAndIncrement();
98112
if (index < midpoint) {
99113
result1.add(next);
@@ -102,53 +116,59 @@ public void whenSplitCollectionHalf_thenConverted() {
102116
}
103117
});
104118

105-
assertTrue(result1.equals(Arrays.asList(1, 2, 3, 4, 5)));
106-
assertTrue(result2.equals(Arrays.asList(6, 7, 8, 9, 10)));
119+
assertTrue(result1.equals(Arrays.asList("Dog", "Cat", "Cow")));
120+
assertTrue(result2.equals(Arrays.asList("Bird", "Moose", "Pig")));
107121
}
108122

109123
@Test
110124
public void whenSplitArrayByWordLength_thenConverted() {
111-
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" };
112-
Map<Integer, List<String>> result = Arrays.stream(words).collect(Collectors.groupingBy(String::length));
125+
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose"};
126+
Map<Integer, List<String>> result = Arrays.stream(animals)
127+
.collect(Collectors.groupingBy(String::length));
113128

114-
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
115-
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
116-
assertTrue(result.get(4).equals(Arrays.asList("cold", "word")));
129+
assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig")));
130+
assertTrue(result.get(4).equals(Arrays.asList("Bird")));
131+
assertTrue(result.get(5).equals(Arrays.asList("Moose")));
117132
}
118133

119134
@Test
120135
public void whenConvertStringToArray_thenConverted() {
121-
String colors = "Red, Blue, Green, Yellow";
122-
String[] result = colors.split(", ");
136+
String animals = "Dog, Cat, Bird, Cow";
137+
String[] result = animals.split(", ");
123138

124-
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
139+
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
125140
}
126141

127142
@Test
128143
public void whenConvertStringToCollection_thenConverted() {
129-
String colors = "Left, Right, Top, Bottom";
130-
Collection<String> result = Arrays.asList(colors.split(", "));
144+
String animals = "Dog, Cat, Bird, Cow";
145+
Collection<String> result = Arrays.asList(animals.split(", "));
131146

132-
assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom")));
147+
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
133148
}
134149

135150
@Test
136151
public void whenConvertStringToMap_thenConverted() {
137-
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
152+
String animals = "1 = Dog, 2 = Cat, 3 = Bird";
138153

139-
Map<Integer, String> result = Arrays.stream(users.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
154+
Map<Integer, String> result = Arrays.stream(
155+
animals.split(", ")).map(next -> next.split(" = "))
156+
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
140157

141-
assertEquals(result.get(1), "John Doe");
142-
assertEquals(result.get(2), "Paul Smith");
143-
assertEquals(result.get(3), "Susan Anderson");
158+
assertEquals(result.get(1), "Dog");
159+
assertEquals(result.get(2), "Cat");
160+
assertEquals(result.get(3), "Bird");
144161
}
145162

146163
@Test
147164
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
148-
String fruits = "Apple. , Orange, Grape. Lemon";
165+
String animals = "Dog. , Cat, Bird. Cow";
149166

150-
Collection<String> result = Arrays.stream(fruits.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList());
167+
Collection<String> result = Arrays.stream(animals.split("[,|.]"))
168+
.map(String::trim)
169+
.filter(next -> !next.isEmpty())
170+
.collect(Collectors.toList());
151171

152-
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
172+
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
153173
}
154174
}

0 commit comments

Comments
 (0)