Java List Join join(List objs, String delim)

Here you can find the source of join(List objs, String delim)

Description

join

License

Open Source License

Declaration

public static <T> String join(List<T> objs, String delim) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;
import java.util.Collection;

import java.util.List;

public class Main {
    public static <T> String join(List<T> objs, String delim) {
        if (objs == null)
            return "";
        return join(objs, delim, 0, objs.size());
    }/*w  w w.jav  a 2 s .  c om*/

    public static <T> String join(List<T> objs, String delim, int start, int end) {
        if (objs == null)
            return "";
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (int i = start; i < end; i++) {
            if (!first)
                sb.append(delim);
            sb.append(objs.get(i));
            first = false;
        }
        return sb.toString();
    }

    /**
     * Provides a string representation of the elements in the given collection.
     *
     * @param c            A collection of elements.  It may be null.
     * @param prefix    String to be used as prefix.
     * @param sep        String to be used to separate elements in the collection.
     * @param suffix    String to be used as suffix.
     * @param <T>        The type of elements in the collection.
     *
     * @return String representation of the elements in the collection.
     */
    public static <T> String toString(Collection<T> c, String prefix, String sep, String suffix) {
        if (c == null || c.size() == 0)
            return prefix + suffix;
        Iterator<T> it = c.iterator();
        String result = prefix + it.next();
        while (it.hasNext())
            result += sep + it.next();
        return result + suffix;
    }

    public static <T> String toString(final Collection<T> a) {
        return toString(a, "", ",", "");
    }

    /**
     * Provides a string representation of the elements in the given array.
     *
     * @param <T>    The type of array elements.
     * @param array  An array of elements.  It may be null.
     * @param prefix String to be used as prefix.
     * @param sep    String to be used to separate array elements.
     * @param suffix String to be used as suffix.
     *
     * @return String representation of the elements in the array.
     */
    public static <T> String toString(T[] array, String prefix, String sep, String suffix) {
        if (array == null || array.length == 0)
            return prefix + suffix;
        String result = prefix + array[0];
        for (int i = 1; i < array.length; i++) {
            result += sep + array[i];
        }
        return result + suffix;
    }

    /**
     * Provides a string representation of the elements in the given array.
     *
     * @param <T>   The type of the array elements.
     * @param array An array of elements.
     *
     * @return string representation of elements in given array.
     */
    public static <T> String toString(final T[] array) {
        return toString(array, "", ",", "");
    }
}

Related

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