Here you can find the source of flattenCollection(Collection> array, String fmt, char separator)
Parameter | Description |
---|---|
array | Array of elements to flatten |
fmt | Format string to use for array flattening |
separator | Separator to use for string concat |
private static String flattenCollection(Collection<?> array, String fmt, char separator)
//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(); } }