Here you can find the source of join(Collection data)
Parameter | Description |
---|---|
data | The collection |
public static String join(Collection data)
//package com.java2s; import java.util.Iterator; import java.util.Collection; public class Main { public static final String DEFAULT_DELIMITER = ","; /** Join the given Object array using the DEFAULT_DELIMITER. //from w ww . ja va2s. c o m @param data The data array @return The resulting String */ public static String join(Object[] data) { return join(data, DEFAULT_DELIMITER); } /** Join the given Object array using the given delimiter. The toString() method on each Object will be invoked to retrieve a String representation of the Object. @param data The data array @param delimiter The delimiter String @return The resulting String */ public static String join(Object[] data, String delimiter) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < data.length; i++) { buffer.append(data.toString()); if (i < data.length - 1) { buffer.append(delimiter); } } return buffer.toString(); } /** Join the given Collection using the DEFAULT_DELIMITER. @param data The collection @return The resulting String */ public static String join(Collection data) { return join(data, DEFAULT_DELIMITER); } /** Join the given Collection using the given delimiter. The toString() method on each Object will be invoked to retrieve a String representation of the Object. @param data The collection @param delimiter The delimiter String @return The resulting String */ public static String join(Collection data, String delimiter) { StringBuffer buffer = new StringBuffer(); Iterator iter = data.iterator(); while (iter.hasNext()) { buffer.append(iter.next().toString()); if (iter.hasNext()) { buffer.append(delimiter); } } return buffer.toString(); } }