Skip to content

Commit fc64b7e

Browse files
author
Hector Romero
committed
BAEL-7: Join and Split Collections from and to String
1 parent b451fcf commit fc64b7e

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.stream.Collectors;
13+
14+
import org.junit.Test;
15+
16+
public class CollectionsToAndFromStringUnitTest {
17+
@Test
18+
public void whenConvertArrayToString_thenConverted() {
19+
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" };
20+
String result = Arrays.stream(colors).collect(Collectors.joining(", "));
21+
22+
assertEquals(result, "Red, Blue, Green, Yellow");
23+
}
24+
25+
@Test
26+
public void whenConvertListToString_thenConverted() {
27+
List<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
28+
String result = directions.stream().collect(Collectors.joining(", "));
29+
30+
assertEquals(result, "Left, Right, Top, Bottom");
31+
}
32+
33+
@Test
34+
public void whenConvertMapToString_thenConverted() {
35+
Map<Integer, String> users = new HashMap<>();
36+
users.put(1, "John Doe");
37+
users.put(2, "Paul Smith");
38+
users.put(3, "Susan Anderson");
39+
40+
String result = users.entrySet().stream()
41+
.map(entry -> entry.getKey() + " = " + entry.getValue())
42+
.collect(Collectors.joining(", "));
43+
44+
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
45+
}
46+
47+
@Test
48+
public void whenConvertNestedListToString_thenConverted() {
49+
List<List<String>> nested = new ArrayList<>();
50+
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
51+
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
52+
53+
String result = nested.stream()
54+
.map(nextList -> nextList.stream()
55+
.collect(Collectors.joining("-")))
56+
.collect(Collectors.joining("; "));
57+
58+
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
59+
}
60+
61+
@Test
62+
public void whenConvertListToStringAndSkipNull_thenConverted() {
63+
List<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
64+
String result = fruits.stream()
65+
.filter(next -> next != null)
66+
.collect(Collectors.joining(", "));
67+
68+
assertEquals(result, "Apple, Orange, Grape");
69+
}
70+
71+
@Test
72+
public void whenConvertStringToArray_thenConverted() {
73+
String colors = "Red, Blue, Green, Yellow";
74+
String[] result = colors.split(", ");
75+
76+
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
77+
}
78+
79+
@Test
80+
public void whenConvertStringToList_thenConverted() {
81+
String colors = "Left, Right, Top, Bottom";
82+
List<String> result = Arrays.asList(colors.split(", "));
83+
84+
assertTrue(result.equals(Arrays.asList("Left", "Right", "Top", "Bottom")));
85+
}
86+
87+
@Test
88+
public void whenConvertStringToMap_thenConverted() {
89+
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
90+
91+
Map<Integer, String> result = Arrays.stream(users.split(", "))
92+
.map(next -> next.split(" = "))
93+
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
94+
95+
assertEquals(result.get(1), "John Doe");
96+
assertEquals(result.get(2), "Paul Smith");
97+
assertEquals(result.get(3), "Susan Anderson");
98+
}
99+
100+
@Test
101+
public void whenConvertListToStringMultipleSeparators_thenConverted() {
102+
String fruits = "Apple. , Orange, Grape. Lemon";
103+
104+
List<String> result = Arrays.stream(fruits.split("[,|.]"))
105+
.map(String::trim).filter(next -> !next.isEmpty())
106+
.collect(Collectors.toList());
107+
108+
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
109+
}
110+
}

0 commit comments

Comments
 (0)