Java tutorial
//package com.java2s; import java.util.Iterator; public class Main { public static String join(final Iterable<?> iterable, String separator) { // handle null, zero and one elements before building a buffer if (iterable == null) { return null; } Iterator<?> iterator = iterable.iterator(); if (!iterator.hasNext()) { return ""; } final Object first = iterator.next(); if (!iterator.hasNext()) { return first.toString(); } // two or more elements final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small if (first != null) { buf.append(first); } while (iterator.hasNext()) { buf.append(separator); final Object obj = iterator.next(); if (obj != null) { buf.append(obj); } } return buf.toString(); } }