Java Iterable Join join(String seprator, Iterable coll)

Here you can find the source of join(String seprator, Iterable coll)

Description

join

License

Apache License

Declaration

public static <V> String join(String seprator, Iterable<V> coll) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static <V> String join(String seprator, Object... arr) {
        return join(seprator, asList(arr));
    }/*w ww  . j  a  v  a2  s  .c o  m*/

    public static <V> String join(String seprator, Iterable<V> coll) {
        return join("", seprator, coll);
    }

    public static <V> String join(String prefix, String seprator, Iterable<V> coll) {
        return join(prefix, false, seprator, coll);
    }

    public static <V> String join(String prefix, boolean bQuote, String seprator, Iterable<V> coll) {
        StringBuffer sb = new StringBuffer(prefix);
        for (Iterator<V> iterator = coll.iterator(); iterator.hasNext();) {
            V v = iterator.next();
            if (bQuote)
                sb.append("'");
            sb.append(v.toString());
            if (bQuote)
                sb.append("'");
            if (iterator.hasNext())
                sb.append(seprator);
        }
        return sb.toString();
    }

    public static <T> List<T> asList(T... objs) {
        return Arrays.asList(objs);
    }

    public static <T> List<T> asList(Collection<T> coll) {
        return new ArrayList<T>(coll);
    }
}

Related

  1. join(String sep, Iterable strings)
  2. join(String separator, Iterable elements)
  3. join(String separator, Iterable objects)
  4. join(String separator, Iterable args)
  5. join(String seperator, Iterator objects)
  6. join(StringBuilder buf, Iterable values, String separator)
  7. joinIterableOnComma(Iterable iterable)
  8. joinIterables(final Iterable... iterables)
  9. joinStr(CharSequence glue, Iterable parts)