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
This commit is contained in:
Mathieu Lirzin
2019-05-25 09:12:05 +00:00
parent ab2fcbcb4a
commit d886fd1b1b
@@ -33,6 +33,7 @@ import java.util.Map;
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 class StringUtil {
}
/**
* 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));
}
/**