BAEL-5754 Convert a List of String to a comma-separated String (#12737)
* BAEL-5617 Handle classes with the same name in Java code samples * BAEL-5617 Handle classes with the same name in Java update project module * BAEL-5754 Convert a List of String to a comma-separated String code samples * BAEL-5754 update unit test class name * BAEL-5754 update unit test methods naming * BAEL-5754 update tests order so to reflect the article
This commit is contained in:
parent
abc3e0235e
commit
e8168990c5
|
@ -56,6 +56,12 @@
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-core</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -80,6 +86,7 @@
|
||||||
<assertj.version>3.22.0</assertj.version>
|
<assertj.version>3.22.0</assertj.version>
|
||||||
<maven.compiler.source.version>17</maven.compiler.source.version>
|
<maven.compiler.source.version>17</maven.compiler.source.version>
|
||||||
<maven.compiler.target.version>17</maven.compiler.target.version>
|
<maven.compiler.target.version>17</maven.compiler.target.version>
|
||||||
|
<spring.version>5.3.22</spring.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,132 @@
|
||||||
|
package com.baeldung.list.tostring;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ListToStringUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingString() {
|
||||||
|
|
||||||
|
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||||
|
|
||||||
|
String commaSeparatedString = String.join(",", arraysAsList);
|
||||||
|
|
||||||
|
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingStringJoiner() {
|
||||||
|
|
||||||
|
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||||
|
|
||||||
|
StringJoiner stringJoiner = new StringJoiner(",");
|
||||||
|
arraysAsList.stream()
|
||||||
|
.forEach(v -> stringJoiner.add(v));
|
||||||
|
String commaSeparatedString = stringJoiner.toString();
|
||||||
|
|
||||||
|
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
StringJoiner stringJoinerWithDelimiterPrefixSuffix = new StringJoiner(",", "[", "]");
|
||||||
|
arraysAsList.stream()
|
||||||
|
.forEach(v -> stringJoinerWithDelimiterPrefixSuffix.add(v));
|
||||||
|
String commaSeparatedStringWithDelimiterPrefixSuffix = stringJoinerWithDelimiterPrefixSuffix.toString();
|
||||||
|
|
||||||
|
assertThat(commaSeparatedStringWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingCollectors() {
|
||||||
|
|
||||||
|
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||||
|
|
||||||
|
String commaSeparatedUsingCollect = arraysAsList.stream()
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
|
||||||
|
assertThat(commaSeparatedUsingCollect).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
String commaSeparatedObjectToString = arraysAsList.stream()
|
||||||
|
.map(Object::toString)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
|
||||||
|
assertThat(commaSeparatedObjectToString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
String commaSeparatedStringValueOf = arraysAsList.stream()
|
||||||
|
.map(String::valueOf)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
|
||||||
|
assertThat(commaSeparatedStringValueOf).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
String commaSeparatedStringValueOfWithDelimiterPrefixSuffix = arraysAsList.stream()
|
||||||
|
.map(String::valueOf)
|
||||||
|
.collect(Collectors.joining(",", "[", "]"));
|
||||||
|
|
||||||
|
assertThat(commaSeparatedStringValueOfWithDelimiterPrefixSuffix).isEqualTo("[ONE,TWO,THREE]");
|
||||||
|
|
||||||
|
String commaSeparatedUsingReduce = arraysAsList.stream()
|
||||||
|
.reduce((x, y) -> x + "," + y)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
assertThat(commaSeparatedUsingReduce).isEqualTo("ONE,TWO,THREE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAListOfString_whenUsingApacheCommonsLang_thenConvertToStringByUsingStringUtils() {
|
||||||
|
|
||||||
|
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||||
|
|
||||||
|
String commaSeparatedString = org.apache.commons.lang3.StringUtils.join(arraysAsList, ",");
|
||||||
|
|
||||||
|
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
String commaSeparatedStringIndex = org.apache.commons.lang3.StringUtils.join(arraysAsList.toArray(), ",", 0, 3);
|
||||||
|
|
||||||
|
assertThat(commaSeparatedStringIndex).isEqualTo("ONE,TWO,THREE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAListOfString_whenUsingSpringCore_thenConvertToStringByUsingStringUtils() {
|
||||||
|
|
||||||
|
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||||
|
|
||||||
|
String collectionToCommaDelimitedString = org.springframework.util.StringUtils.collectionToCommaDelimitedString(arraysAsList);
|
||||||
|
|
||||||
|
assertThat(collectionToCommaDelimitedString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
String collectionToDelimitedString = org.springframework.util.StringUtils.collectionToDelimitedString(arraysAsList, ",");
|
||||||
|
|
||||||
|
assertThat(collectionToDelimitedString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAListOfString_whenUsingGoogleGuava_thenConvertToStringByUsingJoiner() {
|
||||||
|
|
||||||
|
List<String> arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
|
||||||
|
|
||||||
|
String commaSeparatedString = com.google.common.base.Joiner.on(",")
|
||||||
|
.join(arraysAsList);
|
||||||
|
|
||||||
|
assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
List<String> arraysAsListWithNull = Arrays.asList("ONE", null, "TWO", null, "THREE");
|
||||||
|
|
||||||
|
String commaSeparatedStringSkipNulls = com.google.common.base.Joiner.on(",")
|
||||||
|
.skipNulls()
|
||||||
|
.join(arraysAsListWithNull);
|
||||||
|
|
||||||
|
assertThat(commaSeparatedStringSkipNulls).isEqualTo("ONE,TWO,THREE");
|
||||||
|
|
||||||
|
String commaSeparatedStringUseForNull = com.google.common.base.Joiner.on(",")
|
||||||
|
.useForNull(" ")
|
||||||
|
.join(arraysAsListWithNull);
|
||||||
|
|
||||||
|
assertThat(commaSeparatedStringUseForNull).isEqualTo("ONE, ,TWO, ,THREE");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue