Java Iterable Join join(Iterable items)

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

Description

Joins the given sequence of strings with a command and space between each consecutive pair.

License

Open Source License

Declaration

public static String join(Iterable<?> items) 

Method Source Code


//package com.java2s;
// under the terms of the GNU Lesser General Public License as published

import java.util.Iterator;

public class Main {
    /**//from  w w w  .  j  ava 2 s.c o m
     * Joins the given sequence of strings with a command and space between each consecutive pair.
     */
    public static String join(Iterable<?> items) {
        return join(items, ", ");
    }

    /**
     * Joins the given sequence of strings, which the given separator string between each
     * consecutive pair.
     */
    public static String join(Iterable<?> items, String sep) {
        Iterator<?> i = items.iterator();
        if (!i.hasNext()) {
            return "";
        }
        StringBuilder buf = new StringBuilder(String.valueOf(i.next()));
        while (i.hasNext()) {
            buf.append(sep).append(i.next());
        }
        return buf.toString();
    }
}

Related

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