Here you can find the source of toString(Collection
public static <T> String toString(Collection<T> collection, String divider)
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Map; public class Main { public static <T> String toString(Collection<T> collection, String divider) { StringBuilder sb = new StringBuilder(); boolean first = true; for (T object : collection) { if (first == false) { sb.append(divider);//from w w w. j av a 2 s . c o m } first = false; sb.append(toString(object, divider)); } return sb.toString(); } public static <T, S> String toString(Map<T, S> map, String divider) { StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<T, S> entry : map.entrySet()) { if (first == false) { sb.append(divider); } first = false; sb.append(toString(entry.getKey(), divider)).append(" => ").append(toString(entry.getValue(), divider)); } return sb.toString(); } public static String toString(Object object, String divider) { if (object == null) { return null; } else if (object instanceof Collection) { Collection collection = (Collection) object; return toString(collection, divider); } else if (object instanceof Map) { Map map = (Map) object; return toString(map, divider); } else { return object.toString(); } } }