Here you can find the source of combineStringList(List
public static String combineStringList(List<String> sList)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; public class Main { public final static String STRING_SPLIT_TOKEN = "--STR--"; public static String combineStringList(List<String> sList) { return combineList(STRING_SPLIT_TOKEN, sList); }//w ww.j a v a2 s. c om public static String combineList(String token, List<String> parts) { return "[" + combine(token, parts.toArray(new String[parts.size()])) + "]"; } public static String combine(String token, String... parts) { int size = 0; for (String p : parts) size += p != null ? p.length() : 0; StringBuffer sb = new StringBuffer(size + (token.length() * parts.length) + 50); for (int i = 0; (i < parts.length); i++) { sb.append(parts[i]); if (i < parts.length - 1) sb.append(token); } return sb.toString(); } /** * returns the length of the given string. * 0 if str is null * @param str * @return */ public static int length(String str) { return str == null ? 0 : str.length(); } public static String toString(Object[] objs, String separatedBy) { if (objs != null) { return toString(Arrays.asList(objs), separatedBy); } else { return ""; } } public static String toString(int[] objs, String separatedBy) { ArrayList<Integer> l = new ArrayList<Integer>(); for (int i : objs) { l.add(i); } return toString(l, separatedBy); } /** * Same as {@link #toString(java.util.Collection ,String) toString} * separated by ", ". * * @param c * @return */ public static String toString(Collection c) { return toString(c, ", "); } /** * Returns a String representation of this collection. * It will use String.valueOf(Object) to convert object to string. * If separatedby is given, the items will be separated by that string. * * @param c * @param separatedBy * @return */ public static String toString(Collection c, String separatedBy) { if (c == null || c.size() == 0) return ""; StringBuffer sb = new StringBuffer(); for (Iterator itr = c.iterator(); itr.hasNext();) { Object o = itr.next(); sb.append(String.valueOf(o)); if (separatedBy != null && itr.hasNext()) { sb.append(separatedBy); } } return sb.toString(); } public static String toString(Object obj) { return obj == null ? "null" : obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } }