Java List Join join(Collection list, String separator)

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

Description

join

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;

import java.util.Collection;

import java.util.Iterator;

public class Main {
    public static String join(Collection<?> list, String separator) {
        if (list == null) {
            return null;
        }//  w  ww .  j  a v a2s .  c  o m
        Iterator<?> iterator = list.iterator();
        // handle null, zero and one elements before building a buffer
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return valueOf(first);
        }
        // two or more elements
        StringBuffer buf = new StringBuffer(256); // Java default is 16,
        // probably too small
        if (first != null) {
            buf.append(first);
        }
        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    /**
     * transform a Obejct 2 a String ,return value is "" when object is null
     * @param obj
     * @return
     * @author qiumm
     */
    public static String valueOf(Object obj) {
        return (obj == null) ? "" : obj.toString();
    }
}

Related

  1. contains(List joinParams, int joinIndex)
  2. getJoinKeyValues(List tuple, List joinKeyIndices)
  3. join(char separator, List seqs)
  4. join(CharSequence delimiter, Iterable list)
  5. join(CharSequence delimiter, List elements)
  6. join(Collection stringList, String seperator)
  7. join(Collection list, String delimiter)
  8. join(Collection list, String seperator)
  9. join(final List list, final String conjunction)