Here you can find the source of join(Collection> collection, String separator)
public static String join(Collection<?> collection, String separator)
//package com.java2s; //License from project: Open Source License import java.util.Iterator; import java.util.Collection; public class Main { public static String join(Collection<?> collection, String separator) { // handle null, zero and one elements before building a buffer if (collection == null) { return null; }// ww w .ja v a2 s .co m Iterator<?> it = collection.iterator(); if (!it.hasNext()) { return ""; } Object first = it.next(); if (!it.hasNext()) { return first instanceof String ? (String) first : first != null ? first.toString() : ""; } // two or more elements StringBuilder sbResult = new StringBuilder(256); if (first != null) { sbResult.append(first); } while (it.hasNext()) { if (separator != null) { sbResult.append(separator); } Object obj = it.next(); if (obj != null) { sbResult.append(obj); } } return sbResult.toString(); } }