Java Collection Element Get getCollectionPreview(final Collection collection, int previewSize)

Here you can find the source of getCollectionPreview(final Collection collection, int previewSize)

Description

Generates a string that enumerates just the first elements of a collection (a preview).

License

Open Source License

Parameter

Parameter Description
collection the collection.
previewSize the number of elements to enumerate.

Return

a string with the first previewSize elements of the collection.

Declaration

public static String getCollectionPreview(final Collection<?> collection, int previewSize) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**//w w  w .j a  v a 2 s . c  o  m
     * Separator string for lists.
     */
    public static final String LIST_SEPARATOR = ",";

    /**
     * Generates a string that enumerates just the first elements of a collection (a preview).
     *
     * @param collection  the collection.
     * @param previewSize the number of elements to enumerate.
     * @return a string with the first <i>previewSize</i> elements of the collection.
     */
    public static String getCollectionPreview(final Collection<?> collection, int previewSize) {
        final Iterator<?> it = collection.iterator();
        final StringBuilder buffer = new StringBuilder();
        if (it.hasNext()) {
            buffer.append('[').append(it.next().toString());
            for (; previewSize > 0 && it.hasNext(); --previewSize)
                buffer.append(LIST_SEPARATOR).append(' ').append(it.next().toString());
            if (it.hasNext())
                buffer.append(" ... ");
            buffer.append(']');
        }
        return buffer.toString();
    }
}

Related

  1. getCollection(final String iText, final int iStartPosition, final Collection iCollection)
  2. getCollection(Object entity)
  3. getCollection(T... items)
  4. getCollectionClass(TT tt)
  5. getCollectionClosingSymbol(Collection col)
  6. getCollectionSize(Collection collection)
  7. getCollectionSize(Collection collection)
  8. getCommaDelimitedString(Collection elements)
  9. getCommaSeparatedString(Collection collection)