Java Collection Concatenate concatenate(final Collection objects, final String separator)

Here you can find the source of concatenate(final Collection objects, final String separator)

Description

concatenate

License

Open Source License

Parameter

Parameter Description
objects a parameter
separator a parameter

Return

String

Declaration

static String concatenate(final Collection<String> objects, final String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/*from w w w . j av  a2s  .  com*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * 
     * @param objects
     * @param separator
     * @return String
     */
    static String concatenate(final Collection<String> objects, final String separator) {
        final StringBuffer buffer = new StringBuffer();

        if (objects != null) {
            for (Iterator<String> iterator = objects.iterator(); iterator.hasNext();) {
                buffer.append(iterator.next());
                buffer.append(separator);
            }
        }

        // Strip out final separator:
        return concatenate(objects, null, separator);
    }

    /**
     * 
     * @param objects
     * @param prefix
     * @param separator
     * @return String
     */
    private static String concatenate(final Collection<String> objects, final String prefix,
            final String separator) {
        final StringBuffer buffer = new StringBuffer();

        if (objects != null) {
            for (Iterator<String> iterator = objects.iterator(); iterator.hasNext();) {
                if (prefix != null) {
                    buffer.append(prefix);
                }
                buffer.append(iterator.next());
                buffer.append(separator);
            }
        }

        // Strip out final separator:
        return buffer.substring(0, (buffer.length() == 0) ? 0 : buffer.lastIndexOf(separator));
    }
}

Related

  1. concatCollection(Collection collection)
  2. concatElements(final Collection c, final String separator)
  3. concatenate(Collection collections)
  4. concatenate(Collection coll)
  5. concatenate(Collection list)
  6. concatenatedLTL(Collection ltl)
  7. concatenateIds(final Collection ids)
  8. concatenateWithComma(Collection collection)
  9. concatEntries(Collection coll, String sep, String lastSep)