Here you can find the source of collectionToString(Collection> elements)
Parameter | Description |
---|---|
elements | the collection to be converted to a string. This argument can be null , in which case the string "null" is returned. |
public static String collectionToString(Collection<?> elements)
//package com.java2s; //License from project: Apache License import java.util.Collection; public class Main { private static final int EXPECTED_ELEMENT_STRING_LENGTH = 32; private static final String NULL_STR = "null"; /**//from w w w. j av a 2 s. co m * Converts the specified {@code Collection} to a string. This method works * exactly the same as the {@link #arrayToString(Object[])} method but * for collections rather than array. The order of the elements depends * on the order the iterator of the collection returns them. * * @param elements the collection to be converted to a string. This argument * can be {@code null}, in which case the string "null" is returned. * @return the string representation of the passed collection. This method * never returns {@code null}. * * @see #arrayToString(Object[]) */ public static String collectionToString(Collection<?> elements) { if (elements == null) { return NULL_STR; } int size = elements.size(); if (size == 0) { return "[]"; } else if (size == 1) { return "[" + elements.toArray()[0] + "]"; } else { StringBuilder result = new StringBuilder(EXPECTED_ELEMENT_STRING_LENGTH * elements.size()); result.append('['); for (Object element : elements) { result.append('\n'); result.append(element); } result.append(']'); return result.toString(); } } }