Java Iterable Join join(final Iterable objects, final CharSequence separator)

Here you can find the source of join(final Iterable objects, final CharSequence separator)

Description

Joins the given collection of Objects using the given separator and each Object's normal toString() method.

License

Open Source License

Parameter

Parameter Description
objects collection of Objects to join
separator separator to use

Return

objects joined using the given separator.

Declaration

public static String join(final Iterable<? extends Object> objects, final CharSequence separator) 

Method Source Code


//package com.java2s;
import java.util.Iterator;

public class Main {
    /**/*from  w  ww  . j  ava 2  s. c  o m*/
     * Joins the given collection of Objects using the given
     * separator and each Object's normal toString() method.
     * <p>
     * For example, joining the collection "a", "b", "c" with "/"
     * gives "a/b/c".
     *
     * @param objects collection of Objects to join
     * @param separator separator to use
     * @return objects joined using the given separator.
     */
    public static String join(final Iterable<? extends Object> objects, final CharSequence separator) {
        StringBuilder result = new StringBuilder();
        for (Iterator<? extends Object> iter = objects.iterator(); iter.hasNext();) {
            result.append(iter.next().toString());
            if (iter.hasNext()) {
                result.append(separator);
            }
        }
        return result.toString();
    }

    /**
     * Same as {@link #join(Iterable, CharSequence)} but simply takes an array
     * of Objects.
     *
     * @param objects array of Objects to join
     * @param separator separator to use
     */
    public static String join(final Object[] objects, final CharSequence separator) {
        return join(objects, separator, 0, objects.length);
    }

    /**
     * Version of {@link #join(Object[], CharSequence)} that allows you to pass in
     * a {@link StringBuilder} that the result will be built up in. This is useful if you need
     * to do add in other stuff later on.
     *
     * @param resultBuilder StringBuilder to append results to
     * @param objects array of Objects to join
     * @param separator separator to use
     */
    public static void join(final StringBuilder resultBuilder, final Object[] objects,
            final CharSequence separator) {
        join(resultBuilder, objects, separator, 0, objects.length);
    }

    /**
     * Version of {@link #join(Object[], CharSequence)} that allows you to specify a range of
     * indices in the array to join. This can be useful in some cases.
     *
     * @param objects array of Objects to join
     * @param separator separator to use
     * @param startIndex first index to join
     * @param endIndex index after last one to join
     */
    public static String join(final Object[] objects, final CharSequence separator, final int startIndex,
            final int endIndex) {
        StringBuilder result = new StringBuilder();
        join(result, objects, separator, startIndex, endIndex);
        return result.toString();
    }

    /**
     * Version of {@link #join(Object[], CharSequence, int, int)} that allows you to pass in
     * a {@link StringBuilder} that the result will be built up in. This is useful if you need
     * to do add in other stuff later on.
     *
     * @param resultBuilder StringBuilder to append results to
     * @param objects array of Objects to join
     * @param separator separator to use
     * @param startIndex first index to join
     * @param endIndex index after last one to join
     */
    public static void join(final StringBuilder resultBuilder, final Object[] objects, final CharSequence separator,
            final int startIndex, final int endIndex) {
        boolean hasDoneFirst = false;
        for (int i = startIndex; i < endIndex; i++) {
            if (hasDoneFirst) {
                resultBuilder.append(separator);
            }
            resultBuilder.append(objects[i].toString());
            hasDoneFirst = true;
        }
    }
}

Related

  1. join(CharSequence delimiter, Iterable elements)
  2. join(CharSequence separator, Iterable strings)
  3. join(final CharSequence sep, final Iterable src)
  4. join(final Iterable elements, final String separator)
  5. join(final Iterable iterable, final CharSequence separator)
  6. join(final Iterable tokens, final String delimiter)
  7. join(final Iterable iterable, final String separator)
  8. join(final Iterable... iterables)
  9. join(final Iterable items, final String separator)