Android Collection Join join(String conjunction, Collection... collections)

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

Description

join

Declaration

@SuppressWarnings("unchecked")
public static <T> String join(String conjunction,
        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);
    }/*www . j a v a  2s  .  c o 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(Collection... collections)
  2. suffixAndJoin(String suffix, String delimiter, Collection values)
  3. toString(Collection collection, String separator)
  4. join(Collection collection, String s)
  5. join(Collection strings, String sep)