Java Collection Print printElement(Collection pCollection, boolean pAllElement)

Here you can find the source of printElement(Collection pCollection, boolean pAllElement)

Description

For Testing use.

License

Open Source License

Parameter

Parameter Description
pA a parameter
pAllElement a parameter

Declaration

public static String printElement(Collection<?> pCollection, boolean pAllElement) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

import java.util.Collection;

import java.util.Iterator;

public class Main {
    public static final String ANEMPTYSPACE = " ";

    /**//from   w  w w  .java 2 s .c  om
     * For Testing use. Output the content of the array to a string. If the all element parameter is
     * false, output only the first 10 elements.
     * 
     * @param pArray
     * @param pAllElement
     * @return
     */
    public static String printElement(long[] pArray, boolean pAllElement) {
        int size = pArray.length;
        if (!pAllElement && size > 10) {
            size = 10;
        }

        if (size != 10) {
            return Arrays.toString(pArray);
        }

        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < size; i++) {
            buf.append(i).append("= ").append(pArray[i]).append(ANEMPTYSPACE);
        }

        return buf.toString();
    }

    /**
     * For Testing use. Output the content of the array to a string. If the all element parameter is
     * false, output only the first 10 elements.
     * 
     * @param pArray
     * @param pAllElement
     * @return
     */
    public static String printElement(Long[] pArray, boolean pAllElement) {
        int size = pArray.length;
        if (!pAllElement && size > 10) {
            size = 10;
        }

        if (size != 10) {
            return Arrays.toString(pArray);
        }

        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < size; i++) {
            buf.append(i).append("= ").append(pArray[i]).append(ANEMPTYSPACE);
        }

        return buf.toString();
    }

    /**
     * For Testing use. Output the content of the collection to a string. If the all element
     * parameter is false, output only the first 10 elements.
     * 
     * @param pA
     * @param pAllElement
     * @return
     */
    public static String printElement(Collection<?> pCollection, boolean pAllElement) {
        int size = pCollection.size();
        if (!pAllElement && size > 10) {
            size = 10;
        }

        StringBuffer buf = new StringBuffer();
        Iterator<?> iter = pCollection.iterator();
        for (int i = 0; i < size; i++) {
            buf.append(i).append("= ").append(iter.next()).append(ANEMPTYSPACE);
        }

        return buf.toString();
    }
}

Related

  1. printCollection(Collection collections)
  2. printCollection(final Collection collection)
  3. printCollection(String name, Collection collection)
  4. printCollectionDebug(Collection c)
  5. printCollectionSorted(Collection l, String indent)
  6. printElementsUnderCollection(Collection collection)
  7. printList(Collection collection)
  8. println(Collection lines, String suffix, String prefix)
  9. printMatrixList( Collection collection)