Java List Join join(List objects, String separator)

Here you can find the source of join(List objects, String separator)

Description

join

License

Apache License

Declaration

public static <T> String join(List<T> objects, String separator) 

Method Source Code

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

import java.util.Arrays;

import java.util.List;

public class Main {
    public static <T> String join(List<T> objects, String separator) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < objects.size(); i++) {
            if (i > 0) {
                result.append(separator);
            }/*from  w  w  w. java2 s.  c o  m*/
            result.append(objects.get(i).toString());
        }
        return result.toString();
    }

    public static String toString(Object object, Class<?> objectClass) {
        if (null == object) {
            return "null";
        }
        final String toString = object.toString();
        if (isStringEmpty(toString)) {
            return "\"\"";
        } else if (String.class.equals(objectClass)) {
            return "\"" + toString + '\"';
        } else {
            return toString;
        }
    }

    /**
     * Returns the string representation of the specified object, transparently
     * handling null references and arrays.
     * 
     * @param obj
     *            the object
     * @return the string representation
     */
    public static String toString(Object obj) {
        String result;
        if (obj != null) {
            if (obj instanceof boolean[]) {
                result = Arrays.toString((boolean[]) obj);
            } else if (obj instanceof byte[]) {
                result = Arrays.toString((byte[]) obj);
            } else if (obj instanceof char[]) {
                result = Arrays.toString((char[]) obj);
            } else if (obj instanceof double[]) {
                result = Arrays.toString((double[]) obj);
            } else if (obj instanceof float[]) {
                result = Arrays.toString((float[]) obj);
            } else if (obj instanceof int[]) {
                result = Arrays.toString((int[]) obj);
            } else if (obj instanceof long[]) {
                result = Arrays.toString((long[]) obj);
            } else if (obj instanceof Object[]) {
                result = Arrays.deepToString((Object[]) obj);
            } else if (obj instanceof short[]) {
                result = Arrays.toString((short[]) obj);
            } else {
                result = obj.toString();
            }
        } else {
            result = "null";
        }
        return result;
    }

    public static boolean isStringEmpty(String s) {
        return s == null || "".equals(s);
    }
}

Related

  1. join(List list, String delim)
  2. join(List list, String delim)
  3. join(List list, String delimiter)
  4. join(List list, String joiner)
  5. join(List list, String separator)
  6. join(List objs, String delim)
  7. join(List target, String splite)
  8. join(List tokens, String delimiter)
  9. join(List values, String separator)