Skip to content

Commit d886fd1

Browse files
committed
Improved: Rewrite ‘StringUtil#join’ method
(OFBIZ-11014) Use streams instead of low-level string builder. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1859921 13f79535-47bb-0310-9956-ffa450edef68
1 parent ab2fcbc commit d886fd1

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

Diff for: framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Set;
3434
import java.util.StringTokenizer;
3535
import java.util.regex.Pattern;
36+
import java.util.stream.Collectors;
3637

3738
import org.apache.commons.codec.DecoderException;
3839
import org.apache.commons.codec.binary.Hex;
@@ -100,25 +101,16 @@ public static String replaceString(String mainString, String oldString, String n
100101
}
101102

102103
/**
103-
* Creates a single string from a Collection of strings seperated by a delimiter.
104-
* @param col a collection of strings to join
105-
* @param delim the delimiter character(s) to use. (null value will join with no delimiter)
106-
* @return a String of all values in the collection seperated by the delimiter
104+
* Creates a single string from a Collection of strings separated by a delimiter.
105+
*
106+
* @param col a collection of strings to join
107+
* @param delim the delimiter character(s) to use. (null value will join with no delimiter)
108+
* @return a String of all values in the collection separated by the delimiter
107109
*/
108-
public static String join(Collection<?> col, String delim) {
109-
if (UtilValidate.isEmpty(col)) {
110-
return null;
111-
}
112-
StringBuilder buf = new StringBuilder();
113-
Iterator<?> i = col.iterator();
114-
115-
while (i.hasNext()) {
116-
buf.append(i.next());
117-
if (i.hasNext()) {
118-
buf.append(delim);
119-
}
120-
}
121-
return buf.toString();
110+
public static String join(Collection<?> col, CharSequence delim) {
111+
return UtilValidate.isEmpty(col)
112+
? null
113+
: col.stream().map(Object::toString).collect(Collectors.joining(delim));
122114
}
123115

124116
/**

0 commit comments

Comments
 (0)