publicstatic String join(final Iterator<?> iterator, final String separator) {
// handle null, zero and one elements before building a buffer if (iterator == null) { returnnull; } if (!iterator.hasNext()) { return EMPTY; } finalObjectfirst= iterator.next(); if (!iterator.hasNext()) { return Objects.toString(first, ""); }
// two or more elements finalStringBuilderbuf=newStringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small if (first != null) { buf.append(first); }
while (iterator.hasNext()) { if (separator != null) { buf.append(separator); } finalObjectobj= iterator.next(); if (obj != null) { buf.append(obj); } } return buf.toString(); }