Skip to content
Permalink
Browse files

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
  • Loading branch information
mthl committed May 25, 2019
1 parent ab2fcbc commit d886fd1b1b7a3f67567604913be7746a3e940eb6
Showing with 10 additions and 18 deletions.
  1. +10 −18 framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java
@@ -33,6 +33,7 @@
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

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

/**
* Creates a single string from a Collection of strings seperated by a delimiter.
* @param col a collection of strings to join
* @param delim the delimiter character(s) to use. (null value will join with no delimiter)
* @return a String of all values in the collection seperated by the delimiter
* Creates a single string from a Collection of strings separated by a delimiter.
*
* @param col a collection of strings to join
* @param delim the delimiter character(s) to use. (null value will join with no delimiter)
* @return a String of all values in the collection separated by the delimiter
*/
public static String join(Collection<?> col, String delim) {
if (UtilValidate.isEmpty(col)) {
return null;
}
StringBuilder buf = new StringBuilder();
Iterator<?> i = col.iterator();

while (i.hasNext()) {
buf.append(i.next());
if (i.hasNext()) {
buf.append(delim);
}
}
return buf.toString();
public static String join(Collection<?> col, CharSequence delim) {
return UtilValidate.isEmpty(col)
? null
: col.stream().map(Object::toString).collect(Collectors.joining(delim));
}

/**

0 comments on commit d886fd1

Please sign in to comment.
You can’t perform that action at this time.