Here you can find the source of join(Collection> c, String insert)
public static String join(Collection<?> c, String insert)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Iterator; public class Main { /**//from w w w .ja v a 2s .c o m * Return the string created by inserting {@code insert} between * the members of {@code c}. * * @doc.test Note that non-string types are converted to Strings: * js> ListUtils.join(Tools.l(1), " + ") * 1.0 * js> ListUtils.join(Tools.l(1, 2), " + ") * 1.0 + 2.0 * js> ListUtils.join(Tools.l(1, 2, 3), " + ") * 1.0 + 2.0 + 3.0 */ public static String join(Collection<?> c, String insert) { return join(c, insert, insert); } /** * Return the string created by inserting {@code insert} between * the members of {@code c}, and {@code preFinal} before the last one. * * @doc.test * js> ListUtils.join(Tools.l("apples"), ", ", " or ") * apples * js> ListUtils.join(Tools.l("apples", "oranges"), ", ", " or ") * apples or oranges * js> ListUtils.join(Tools.l("apples", "oranges", "pears"), ", ", " or ") * apples, oranges or pears * @doc.test Non-string types are converted to strings: * js> ListUtils.join(Tools.l(1, 2, 3), " + ", " - ") * 1.0 + 2.0 - 3.0 */ public static String join(Collection<?> c, String insert, String preFinal) { StringBuilder result = new StringBuilder(); boolean first = true; for (Iterator<?> it = c.iterator(); it.hasNext(); first = false) { String s = it.next().toString(); if (!first) { result.append(it.hasNext() ? insert : preFinal); } result.append(s); } return result.toString(); } }