Here you can find the source of join(Collection
Parameter | Description |
---|---|
collection | A collection of items to join |
joinString | The string to place between the items |
public static <T> String join(Collection<T> collection, String joinString)
//package com.java2s; import java.util.Collection; public class Main { /**/* w w w . ja va2s. co m*/ * Join a collection of items into a string. * @param collection A collection of items to join * @param joinString The string to place between the items * @return A string consisting of the string representations of the items in * collection, concatenated, with joinString interspersed between them. */ public static <T> String join(Collection<T> collection, String joinString) { StringBuilder result = new StringBuilder(); boolean isFirst = true; for (final T elem : collection) { if (!isFirst) { result.append(joinString); } else { isFirst = false; } result.append(elem.toString()); } return result.toString(); } /** * Convert the given value to a string value, or simply return an empty string * if the specified value is <code>null</code>. * @param value * @return String */ public static String toString(Object value) { return value == null ? "" : value.toString(); } /** * Convert the given value to a string value, or simply return the default string * if the specified value is <code>null</code>. * @param value * @param defaultString * @return String */ public static String toString(Object value, String defaultString) { return value == null ? defaultString : value.toString(); } }