Java List Join join(String separator, List items)

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

Description

Returns a string containing the string representation of each item in the given list, separated by the given separator.

License

Apache License

Declaration

public static String join(String separator, List<?> items) 

Method Source Code


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

import java.util.Arrays;
import java.util.List;

public class Main {
    /**/*from   w  w w  . j  ava2 s  .  c o  m*/
     * Returns a string containing the string representation of each item in the
     * given list, separated by the given separator.
     */
    public static String join(String separator, List<?> items) {

        assert separator != null;
        assert items != null;

        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Object item : items) {

            if (!first) {
                sb.append(separator);
            }

            sb.append(item);

            first = false;
        }

        return sb.toString();
    }

    /**
     * Returns a string containing the string representation of each item in the
     * given list, separated by the given separator.
     */
    public static String join(String separator, Object... items) {

        assert items != null;

        return join(separator, Arrays.asList(items));
    }

    /**
     * Array-aware toString method. If the given value is an array, passes it to
     * one of the toString classes in the JDK's Arrays class, else returns the
     * result of its toString method.
     */
    public static String toString(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof boolean[]) {
            return Arrays.toString((boolean[]) value);
        } else if (value instanceof char[]) {
            return Arrays.toString((char[]) value);
        } else if (value instanceof byte[]) {
            return Arrays.toString((byte[]) value);
        } else if (value instanceof short[]) {
            return Arrays.toString((short[]) value);
        } else if (value instanceof int[]) {
            return Arrays.toString((int[]) value);
        } else if (value instanceof long[]) {
            return Arrays.toString((long[]) value);
        } else if (value instanceof float[]) {
            return Arrays.toString((float[]) value);
        } else if (value instanceof double[]) {
            return Arrays.toString((double[]) value);
        } else if (value instanceof Object[]) {
            return Arrays.toString((Object[]) value);
        } else {
            return value.toString();
        }
    }
}

Related

  1. join(String separator, Collection list)
  2. join(String separator, Collection inputList)
  3. join(String separator, List items)
  4. join(String separator, List objs)
  5. join(String separator, List elements)
  6. join(String separator, List parts)
  7. join(String separator, List parts)
  8. join(String separator, List parts)
  9. join(String sign, List src)