Java Collection Flatten flattenCollection(Collection array, String fmt, char separator)

Here you can find the source of flattenCollection(Collection array, String fmt, char separator)

Description

Used for flattening a collection of objects into a string

License

Open Source License

Parameter

Parameter Description
array Array of elements to flatten
fmt Format string to use for array flattening
separator Separator to use for string concat

Return

Representative string made up of array elements

Declaration

private static String flattenCollection(Collection<?> array, String fmt, char separator) 

Method Source Code

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

import java.util.Collection;

public class Main {
    /**//from  w w  w  .j  a  v a  2 s.c  o  m
     * Used for flattening a collection of objects into a string
     * @param   array   Array of elements to flatten
     * @param   fmt Format string to use for array flattening
     * @param   separator   Separator to use for string concat
     * @return   Representative string made up of array elements */
    private static String flattenCollection(Collection<?> array, String fmt, char separator) {
        StringBuilder builder = new StringBuilder();

        //append all elements in the array into a string
        for (Object element : array) {
            String elemValue = null;

            //replace null values with empty string to maintain index order
            if (null == element)
                elemValue = "";
            else
                elemValue = element.toString();

            builder.append(String.format(fmt, elemValue, separator));
        }

        //remove the last separator, if appended
        if ((builder.length() > 1) && (builder.charAt(builder.length() - 1) == separator))
            builder.deleteCharAt(builder.length() - 1);

        return builder.toString();
    }
}

Related

  1. flatten(Collection> collOfLists)
  2. flatten(Collection collection, char separator)
  3. flatten(Collection original)
  4. flatten(final S targetCollection, final Collection> setOfSets)
  5. flattenCollection(Collection col)
  6. flattenForest(Collection forest)
  7. flattenNodeIds( Collection> nodesInMotif)
  8. flattenStrings(Collection values)
  9. flattenToString(Collection c)