Java Iterable Join join(Iterable items, String separator)

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

Description

Same as #join(String[],String) but expecting an Iterable instead of an array

License

Apache License

Declaration

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

Method Source Code

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

import java.util.Arrays;
import java.util.Iterator;

public class Main {
    /**/* w w w .  j av a 2 s .  com*/
     * Creates a string by concatenating items array of specified items,
     * separating each with the specified separator.
     */
    public static String join(String[] items, String separator) {
        return join(Arrays.asList(items), separator);
    }

    /**
     * Same as {@link #join(String[], String)} but expecting an {@link Iterable}
     * instead of an array
     */
    public static String join(Iterable<?> items, String separator) {
        final StringBuilder builder = new StringBuilder();
        final Iterator<?> it = items.iterator();
        while (it.hasNext()) {
            builder.append(it.next().toString());
            if (it.hasNext()) {
                builder.append(separator);
            }
        }
        return builder.toString();
    }
}

Related

  1. join(Iterable elements, String separator)
  2. join(Iterable elements, String separator)
  3. join(Iterable items)
  4. join(Iterable items)
  5. join(Iterable items, String delimiter)
  6. join(Iterable iterable, String separator)
  7. join(Iterable iterable, String separator)
  8. join(Iterable list, String delimiter)
  9. join(Iterable objects)