Android Collection Join join(Collection... collections)

Here you can find the source of join(Collection... collections)

Description

join

Declaration

public static <T> String join(Collection<T>... collections) 

Method Source Code

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

public class Main {

    public static <T> String join(Collection<T>... collections) {
        return join(",", collections);
    }/*w w  w.j av  a 2  s. co m*/

    @SuppressWarnings("unchecked")
    public static <T> String join(String conjunction,
            Collection<T>... collections) {

        int length = collections.length;

        if (length == 0)
            return null;

        Collection<Object> collection = (Collection<Object>) collections[0];

        if (length > 1) {
            for (int i = 1; i < length; i++) {
                collection.addAll(collections[i]);
            }
        }

        StringBuilder sb = new StringBuilder();

        boolean first = true;

        for (Object obj : collection) {
            if (obj == null)
                break;

            if (first)
                first = false;
            else
                sb.append(conjunction);

            sb.append(obj.toString());
        }

        return sb.toString();
    }
}

Related

  1. join(String conjunction, Collection... collections)
  2. suffixAndJoin(String suffix, String delimiter, Collection values)
  3. toString(Collection collection, String separator)
  4. join(Collection collection, String s)