Java Collection Join join(Collection col, String separator)

Here you can find the source of join(Collection col, String separator)

Description

join

License

Open Source License

Declaration

public static String join(Collection<?> col, String separator) 

Method Source Code


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

public class Main {
    public static String join(Object[] arr, String separator) {
        StringBuilder sb = new StringBuilder();
        if (arr.length > 0) {
            sb.append(arr[0].toString());
        }//from   w ww. jav  a2 s  .  com
        for (int i = 1; i < arr.length; i++) {
            sb.append(separator);
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    public static String join(Collection<?> col, String separator) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> it = col.iterator();
        if (!col.isEmpty()) {
            Object obj = it.next();
            sb.append(obj.toString());
        }
        while (it.hasNext()) {
            Object obj = it.next();
            sb.append(separator);
            sb.append(obj.toString());
        }
        return sb.toString();
    }

    public static String join(Object[] arr) {
        return join(arr, ",");
    }

    public static String join(Collection<?> arr) {
        return join(arr, ",");
    }

    public static boolean isEmpty(String s) {

        return s == null || s.isEmpty();
    }
}

Related

  1. join(Collection c, String delim)
  2. join(Collection c, String delimiter)
  3. join(Collection c, String insert)
  4. join(Collection c, String separator)
  5. join(Collection col, String delim)
  6. join(Collection coll, String delim)
  7. join(Collection collection, String delimiter)
  8. join(Collection collection, String delimiter)
  9. join(Collection collection, String inputSeparator)