Java Iterable Join join(final Iterable iterable, final String separator)

Here you can find the source of join(final Iterable iterable, final String separator)

Description

Copied fom commons StringUtils

Joins the elements of the provided Iterable into a single String containing the provided elements.

License

Apache License

Declaration

public static String join(final Iterable<?> iterable, final String separator) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;

public class Main {
    /**//from  ww w . j a  v  a2  s . c  o m
     * Copied fom commons StringUtils
     * <p>Joins the elements of the provided {@code Iterable} into
     * a single String containing the provided elements.</p>
     */
    public static String join(final Iterable<?> iterable, final String separator) {
        if (iterable == null) {
            return null;
        }
        return join(iterable.iterator(), separator);
    }

    /**
     * Copied fom commons StringUtils
     */
    public static String join(final Iterator<?> iterator, final String separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return "";
        }
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first == null ? null : first.toString();
        }

        // two or more elements
        final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }
}

Related

  1. join(final Iterable objects, final CharSequence separator)
  2. join(final Iterable tokens, final String delimiter)
  3. join(final Iterable iterable, final String separator)
  4. join(final Iterable... iterables)
  5. join(final Iterable items, final String separator)
  6. join(final Iterable container)
  7. join(final Iterable iterable, final String joinString)
  8. join(final Iterable objs, final String delimiter)
  9. join(final Iterable objs, final String delimiter)