HADOOP-7451. Generalize StringUtils#join. Contributed by Chris Douglas.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1143624 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Foley 2011-07-07 01:31:58 +00:00
parent d381ff25f4
commit f9e04d7544
2 changed files with 12 additions and 10 deletions

View File

@ -239,6 +239,8 @@ Trunk (unreleased changes)
HADOOP-7429. Add another IOUtils#copyBytes method. (eli)
HADOOP-7451. Generalize StringUtils#join. (Chris Douglas via mattf)
OPTIMIZATIONS
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole

View File

@ -29,6 +29,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
@ -741,16 +742,15 @@ public class StringUtils {
* @param separator Separator to join with.
* @param strings Strings to join.
*/
public static String join(CharSequence separator, Iterable<String> strings) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String s : strings) {
if (first) {
first = false;
} else {
sb.append(separator);
}
sb.append(s);
public static String join(CharSequence separator, Iterable<?> strings) {
Iterator<?> i = strings.iterator();
if (!i.hasNext()) {
return "";
}
StringBuilder sb = new StringBuilder(i.next().toString());
while (i.hasNext()) {
sb.append(separator);
sb.append(i.next().toString());
}
return sb.toString();
}