Java Iterable to Collection iterableToString(String separator, Iterable collection)

Here you can find the source of iterableToString(String separator, Iterable collection)

Description

iterable To String

License

Open Source License

Declaration

public static final <T> 
    String iterableToString(String separator, Iterable<T> collection) 

Method Source Code

//package com.java2s;

import java.util.Iterator;

public class Main {
    public static final <T> //
    String iterableToString(String separator, Iterable<T> collection) {
        if (collection != null)
            return iterableToString(separator, collection.iterator());
        return "";
    }//www.  ja  v  a  2s.c o m

    public static final <T> //
    String iterableToString(String separator, Iterator<T> iterator) {
        StringBuilder sb = new StringBuilder();
        while (iterator.hasNext()) {
            T item = iterator.next();
            if (item != null) {
                String appendable = item.toString();
                sb.append(appendable);
                if (iterator.hasNext())
                    sb.append(separator);
            }
        }
        return sb.toString();
    }

    public static final <T> //
    String iterableToString(String separator, T... collection) {
        StringBuilder sb = new StringBuilder();
        if (collection != null)
            for (int i = 0; i != collection.length; i++) {
                T item = collection[i];
                if (item != null) {
                    String appendable = item.toString();
                    sb.append(appendable);
                    if (i != collection.length - 1)
                        sb.append(separator);
                }
            }
        return sb.toString();
    }
}

Related

  1. iterableToCollection(Iterable iter, Collection list)
  2. iterableToCollection(Iterable iterable)
  3. iterableToString(Iterable collection, String sep)