Java Utililty Methods Collection Flatten

List of utility methods to do Collection Flatten

Description

The list of methods to do Collection Flatten are organized into topic(s).

Method

Sflatten(final S targetCollection, final Collection> setOfSets)
flatten
for (Collection<? extends T> innerSet : setOfSets)
    targetCollection.addAll(innerSet);
return targetCollection;
CollectionflattenCollection(Collection col)
flatten Collection
Collection toRet = null;
try {
    toRet = col.getClass().newInstance();
} catch (InstantiationException e) {
    e.printStackTrace(); 
} catch (IllegalAccessException e) {
    e.printStackTrace(); 
for (Object o : col) {
    if (o instanceof Collection) {
        toRet.addAll(flattenCollection((Collection) o));
    } else {
        toRet.add(o);
return toRet;
StringflattenCollection(Collection array, String fmt, char separator)
Used for flattening a collection of objects into a string
StringBuilder builder = new StringBuilder();
for (Object element : array) {
    String elemValue = null;
    if (null == element)
        elemValue = "";
    else
        elemValue = element.toString();
    builder.append(String.format(fmt, elemValue, separator));
...
CollectionflattenForest(Collection forest)
flatten Forest
final Collection ret = new LinkedHashSet();
for (final Iterator it = forest.iterator(); it.hasNext();) {
    final Collection set = (Collection) it.next();
    ret.addAll(set);
return ret;
SetflattenNodeIds(Collection> nodesInMotif)
flatten Node Ids
Set<Long> nodes = new HashSet<Long>();
synchronized (nodesInMotif) {
    try {
        for (Set<Long> motifNodes : nodesInMotif) {
            nodes.addAll(motifNodes);
    } catch (ConcurrentModificationException cme) {
        System.err.println("Error occurred " + cme.getMessage());
...
StringflattenStrings(Collection values)
Flatten the list of strings into a single string with a space separator.
return flattenStrings(values, " ");
StringflattenToString(Collection c)
Creates a comma separated list of all values held in the given collection.
return flattenToString(c, ",");
StringflattenToString(Collection collection, String delimiter)
flatten To String
StringBuffer buffer = new StringBuffer();
for (Object obj : collection) {
    buffer.append(obj.toString());
    buffer.append(',');
if (buffer.length() > 0) {
    buffer.setLength(buffer.length() - 1);
return buffer.toString();