Java List Join join(List strings, String delim)

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

Description

join

License

Open Source License

Declaration

public static String join(List<String> strings, String delim) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Iterator;

import java.util.List;

public class Main {
    public static final String EMPTY = "";

    public static String join(List<String> strings, String delim) {
        return join(strings, 0, strings.size(), delim);
    }/*from   w ww.  j  a  v a 2s  .c  o m*/

    public static String join(List<String> strings, int off, String delim) {
        return join(strings, off, strings.size() - off, delim);
    }

    public static String join(List<String> strings, int off, int len, String delim) {
        if (strings.size() <= 0)
            return EMPTY;
        else if (strings.size() == 1)
            return strings.get(0);

        StringBuilder sb = new StringBuilder(256);
        if (off == 0 && !(strings instanceof ArrayList)) {
            // performance optimization primarily for LinkedLists
            Iterator<String> it = strings.iterator();
            if (it.hasNext()) {
                sb.append(it.next());
                for (int i = 1; i < len && it.hasNext(); i++) {
                    sb.append(delim);
                    sb.append(it.next());
                }
            }
        } else {
            sb.append(strings.get(off));
            off++;
            for (int i = 0; i < len && off < strings.size(); i++, off++) {
                sb.append(delim);
                sb.append(strings.get(off));
            }
        }
        return sb.toString();
    }

    public static String join(String[] parts, String delim) {
        return join(parts, 0, parts.length, delim);
    }

    public static String join(String[] parts, int off, String delim) {
        return join(parts, off, parts.length - off, delim);
    }

    public static String join(String[] parts, int off, int len, String delim) {
        if (len <= 0)
            return EMPTY;
        else if (len == 1)
            return parts[off];

        StringBuilder sb = new StringBuilder(256);
        sb.append(parts[off]);
        off++;
        for (int i = 0; i < len && off < parts.length; i++, off++) {
            sb.append(delim);
            sb.append(parts[off]);
        }
        return sb.toString();
    }

    public static String toString(char c) {
        return new String(new char[] { c });
    }

    public static String toString(int[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(short[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(float[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(long[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(double[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(boolean[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(char[] o) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(o[0]);
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(o[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    public static String toString(Object o) {
        return o == null ? "null" : o.toString();
    }

    public static String toString(Object[] o) {
        if (isEmpty(o))
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        sb.append(toString(o[0]));
        for (int i = 1; i < o.length; i++) {
            sb.append(',');
            sb.append(toString(o[i]));
        }
        sb.append(']');
        return sb.toString();
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isEmpty(Object[] o) {
        return o == null || o.length == 0;
    }
}

Related

  1. join(List s, String delim)
  2. join(List s, String sep)
  3. join(List strings)
  4. join(List strings)
  5. join(List strings)
  6. join(List strings, String delimiter)
  7. join(List strings, String join)
  8. join(List strings, String joiner)
  9. join(List strings, String sep)