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-7429. Add another IOUtils#copyBytes method. (eli)
HADOOP-7451. Generalize StringUtils#join. (Chris Douglas via mattf)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole 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.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -741,16 +742,15 @@ public class StringUtils {
* @param separator Separator to join with. * @param separator Separator to join with.
* @param strings Strings to join. * @param strings Strings to join.
*/ */
public static String join(CharSequence separator, Iterable<String> strings) { public static String join(CharSequence separator, Iterable<?> strings) {
StringBuilder sb = new StringBuilder(); Iterator<?> i = strings.iterator();
boolean first = true; if (!i.hasNext()) {
for (String s : strings) { return "";
if (first) {
first = false;
} else {
sb.append(separator);
} }
sb.append(s); StringBuilder sb = new StringBuilder(i.next().toString());
while (i.hasNext()) {
sb.append(separator);
sb.append(i.next().toString());
} }
return sb.toString(); return sb.toString();
} }