Here you can find the source of join(Collection> iterable, String delimiter)
NOTE: An will be thrown if you pass in null for the collection of items or the delimiter.
public static String join(Collection<?> iterable, String delimiter)
//package com.java2s; //License from project: Apache License import java.util.Collection; public class Main { /**// ww w .j av a 2 s. c om * @return A string that contains the items in the given collection with the given delimiter between values. The * delimiter will not appear as a prefix to the returned string or as a suffix, it is only used in between * two items. The items in the list are turned into strings via {@link String#valueOf(Object)}. This is * simply a helper for calling {@link #join(Collection, String, String, String)}, passing in null for * prefix and suffix. * <p>NOTE: An {@link IllegalArgumentException} will be thrown if you pass in null for the collection of * items or the delimiter. */ public static String join(Collection<?> iterable, String delimiter) { return join(iterable, delimiter, null, null); } /** * @return A string that contains the items in the given collection with the given delimiter between values, the * given prefix before any of the items, and the given suffix after all the items. The delimiter will not * appear as a prefix to the returned string or as a suffix, it is only used in between two items. The * items in the list are turned into strings via {@link String#valueOf(Object)}. You can safely pass in * null for the prefix and/or suffix and they will be turned into the empty string, effectively removing * them from the returned string. * <p>NOTE: An {@link IllegalArgumentException} will be thrown if you pass in null for the collection of * items or the delimiter. */ public static String join(Collection<?> iterable, String delimiter, String prefix, String suffix) { if (iterable == null) throw new IllegalArgumentException("iterable cannot be null"); if (delimiter == null) throw new IllegalArgumentException("delimiter cannot be null"); if (prefix == null) prefix = ""; if (suffix == null) suffix = ""; StringBuilder sb = new StringBuilder(); // Add the prefix sb.append(prefix); // Add each item, with the delimiter between items. boolean firstItem = true; for (Object obj : iterable) { if (!firstItem) sb.append(delimiter); sb.append(String.valueOf(obj)); firstItem = false; } // Add the suffix sb.append(suffix); return sb.toString(); } }