diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/text/StrJoiner.java b/hutool-core/src/main/java/org/dromara/hutool/core/text/StrJoiner.java index a686a5437..413742bcc 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/text/StrJoiner.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/text/StrJoiner.java @@ -145,7 +145,7 @@ public class StrJoiner implements Appendable, Serializable { /** * 设置分隔符 * - * @param delimiter 分隔符 + * @param delimiter 分隔符,{@code null}表示无连接符,直接拼接 * @return this */ public StrJoiner setDelimiter(final CharSequence delimiter) { @@ -422,7 +422,9 @@ public class StrJoiner implements Appendable, Serializable { */ private Appendable prepare() throws IOException { if (hasContent) { - this.appendable.append(delimiter); + if(null != delimiter){ + this.appendable.append(delimiter); + } } else { if (null == this.appendable) { this.appendable = new StringBuilder(); diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayUtilTest.java index 6f3f41e16..cde895d97 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/array/ArrayUtilTest.java @@ -13,7 +13,6 @@ package org.dromara.hutool.core.array; import org.dromara.hutool.core.collection.ListUtil; -import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.util.CharsetUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -26,10 +25,9 @@ import java.util.*; * * @author Looly */ -@SuppressWarnings("ConstantValue") public class ArrayUtilTest { - @SuppressWarnings("DataFlowIssue") + @SuppressWarnings({"DataFlowIssue", "ConstantValue"}) @Test public void isEmptyTest() { final int[] a = {}; @@ -330,6 +328,41 @@ public class ArrayUtilTest { Assertions.assertEquals("aa,bb,cc,dd", join2); } + @Test + public void testJoinWithNullElement() { + final String[] array = { "Java", null, "Python" }; + final String result = ArrayUtil.join(array, ", ", value -> value == null ? "null" : value); + Assertions.assertEquals("Java, null, Python", result); + } + + @Test + public void testJoinWithEmptyArray() { + final String[] array = {}; + final String result = ArrayUtil.join(array, ", ", String::toUpperCase); + Assertions.assertEquals("", result); + } + + @Test + public void testJoinWithoutEditor() { + final Integer[] array = { 1, 2, 3 }; + final String result = ArrayUtil.join(array, ", "); + Assertions.assertEquals("1, 2, 3", result); + } + + @Test + public void testJoinWithEditor() { + final String[] array = { "java", "scala", "kotlin" }; + final String result = ArrayUtil.join(array, " -> ", String::toUpperCase); + Assertions.assertEquals("JAVA -> SCALA -> KOTLIN", result); + } + + @Test + public void testJoinWithNullConjunction() { + final String[] array = { "one", "two", "three" }; + final String result = ArrayUtil.join(array, null, value -> value + "!"); + Assertions.assertEquals("one!two!three!", result); + } + @Test public void getArrayTypeTest() { Class arrayType = ArrayUtil.getArrayType(int.class); @@ -656,7 +689,6 @@ public class ArrayUtilTest { void setOrPaddingTest(){ final String[] arr = new String[0]; final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good"); - Console.log(newArr); Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr); }