Here you can find the source of join(Iterable
public static String join(Iterable<String> array)
//package com.java2s; //License from project: Open Source License import java.util.Iterator; public class Main { public static String join(Iterable<String> array) { return join(array, ","); }// www. j a v a 2s .c o m public static String join(Iterable<String> array, String delimiter) { if (array == null) return ""; StringBuilder result = new StringBuilder(); Iterator<String> it = array.iterator(); boolean first = true; while (it.hasNext()) { if (first) first = false; else result.append(delimiter); result.append(it.next()); } return result.toString(); } public static String join(Object[] array) { return join(array, ","); } public static String join(Object[] array, String delimiter) { if (array == null) return ""; // Cache the length of the delimiter // has the side effect of throwing a NullPointerException if // the delimiter is null. int delimiterLength = delimiter.length(); // Nothing in the array return empty string // has the side effect of throwing a NullPointerException if // the array is null. if (array.length == 0) { return ""; } // Only one thing in the array, return it. if (array.length == 1) { if (array[0] == null) { return ""; } return array[0].toString(); } // Make a pass through and determine the size // of the resulting string. int length = 0; for (int i = 0; i < array.length; i++) { if (array[i] != null) { length += array[i].toString().length(); } if (i < array.length - 1) { length += delimiterLength; } } // Make a second pass through and concatenate everything // into a string buffer. StringBuilder result = new StringBuilder(length); for (int i = 0; i < array.length; i++) { if (array[i] != null) { result.append(array[i].toString()); } if (i < array.length - 1) { result.append(delimiter); } } return result.toString(); } }