Here you can find the source of join(final List
Parameter | Description |
---|---|
delimiter | string to insert between list items. |
list | items to join. |
public static String join(final List<Object> list, final String delimiter)
//package com.java2s; //License from project: Open Source License import java.util.Iterator; import java.util.List; public class Main { /**//w w w.ja v a 2 s .co m * Join objects in a list using the specified delimiter. The objects * toString method is used to get a string value. * * @param delimiter * string to insert between list items. * @param list * items to join. * @return string containing delimiter delimited list items. */ public static String join(final List<Object> list, final String delimiter) { if (list == null || list.size() == 0) { return ""; } StringBuffer buf = new StringBuffer(); Iterator<Object> iter = list.iterator(); // add first element to buffer if (iter.hasNext()) { buf.append(iter.next().toString()); } // add delimiter before other elements while (iter.hasNext()) { buf.append(delimiter); buf.append(iter.next().toString()); } return buf.toString(); } }