From e8168990c5419748c1a5c3012131a3f7ac091861 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bogdan=20Cardo=C5=9F?=
<106325528+sodrac@users.noreply.github.com>
Date: Fri, 30 Sep 2022 07:38:08 +0300
Subject: [PATCH] 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
---
.../core-java-collections-list-4/pom.xml | 7 +
.../list/tostring/ListToStringUnitTest.java | 132 ++++++++++++++++++
2 files changed, 139 insertions(+)
create mode 100644 core-java-modules/core-java-collections-list-4/src/test/java/com/baeldung/list/tostring/ListToStringUnitTest.java
diff --git a/core-java-modules/core-java-collections-list-4/pom.xml b/core-java-modules/core-java-collections-list-4/pom.xml
index 773f8c105b..49b3f849d4 100644
--- a/core-java-modules/core-java-collections-list-4/pom.xml
+++ b/core-java-modules/core-java-collections-list-4/pom.xml
@@ -56,6 +56,12 @@
${assertj.version}
test
+
+ org.springframework
+ spring-core
+ ${spring.version}
+ test
+
@@ -80,6 +86,7 @@
3.22.0
17
17
+ 5.3.22
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-list-4/src/test/java/com/baeldung/list/tostring/ListToStringUnitTest.java b/core-java-modules/core-java-collections-list-4/src/test/java/com/baeldung/list/tostring/ListToStringUnitTest.java
new file mode 100644
index 0000000000..479ccaf92f
--- /dev/null
+++ b/core-java-modules/core-java-collections-list-4/src/test/java/com/baeldung/list/tostring/ListToStringUnitTest.java
@@ -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 arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
+
+ String commaSeparatedString = String.join(",", arraysAsList);
+
+ assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
+ }
+
+ @Test
+ void givenAListOfString_whenUsingJava8_thenConvertToStringByUsingStringJoiner() {
+
+ List 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 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 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 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 arraysAsList = Arrays.asList("ONE", "TWO", "THREE");
+
+ String commaSeparatedString = com.google.common.base.Joiner.on(",")
+ .join(arraysAsList);
+
+ assertThat(commaSeparatedString).isEqualTo("ONE,TWO,THREE");
+
+ List 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");
+ }
+
+}