Here you can find the source of valueOfCollection(Collection> c, int maxLength)
public static String valueOfCollection(Collection<?> c, int maxLength)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { /** This returns a String representation of a Collection, with a maximum length specified. This is useful in getting a string representation of a potentially very large collection object without running out of memory. * It protects against large numbers of elements in collections, but cannot protect against very large single elements in collections whose toString() method exceeds available memory. */ public static String valueOfCollection(Collection<?> c, int maxLength) { if (c == null) return ""; Iterator<?> i = c.iterator(); if (!i.hasNext()) return "[]"; StringBuilder sb = new StringBuilder((int) (maxLength * 1.5)); sb.append('['); for (;;) { Object e = i.next();/*w w w .ja v a 2 s . co m*/ String s; if (e == c) { s = "(this Collection)"; } else { s = String.valueOf(e); if (s.length() > maxLength) { s = s.substring(0, maxLength - 1); } } sb.append(s); if (sb.length() >= maxLength) { if (sb.length() > maxLength) { sb.delete(1000, sb.length() - 1); } return sb.append("<truncated...>").toString(); } else if (!i.hasNext()) { return sb.append(']').toString(); } sb.append(", "); } } /** This works the same as {@link String#valueOf(Object)}, except it returns an empty string instead of "null" if the input is null. */ public static String valueOf(Object o) { return o == null ? "" : String.valueOf(o); } }