Here you can find the source of join(Collection> collection, String delimiter)
Parameter | Description |
---|---|
collection | Collection of objects |
delimiter | (optional) String to separate the items of the collection in the joined string - <code>null</code> is interpreted as empty string |
public static String join(Collection<?> collection, String delimiter)
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.Iterator; public class Main { private final static String EMPTY_STRING = ""; /**/*from w w w.ja v a2 s .c o m*/ * Joins a Collection of typed objects using their toString method, separated by delimiter * * @param collection Collection of objects * @param delimiter (optional) String to separate the items of the collection in the joined string - <code>null</code> is interpreted as empty string * @return the joined string */ public static String join(Collection<?> collection, String delimiter) { if (collection == null || collection.isEmpty()) { return EMPTY_STRING; } else if (delimiter == null) { delimiter = EMPTY_STRING; } StringBuilder builder = new StringBuilder(); Iterator<?> it = collection.iterator(); while (it.hasNext()) { builder.append(it.next()).append(delimiter); } int length = builder.length(); builder.delete(length - delimiter.length(), length); return builder.toString(); } }