Java Collection Combine combine(String separator, Collection stringCollection)

Here you can find the source of combine(String separator, Collection stringCollection)

Description

Combines a collection of strings into a single string, separated by the given character set

License

Open Source License

Parameter

Parameter Description
separator character set included between each part of the given array
stringCollection collection of strings to combine

Return

the combined string

Declaration

public static String combine(String separator, Collection<String> stringCollection) 

Method Source Code

//package com.java2s;
/*/*  w w  w  .j a  va  2 s  .co  m*/
 * This file is part of Commodus.
 *
 * Commodus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Commodus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Commodus.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collection;

public class Main {
    /**
     * Represents an empty array of strings
     */
    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * Combines a collection of strings into a single string, separated by the given character set
     *
     * @param separator        character set included between each part of the given array
     * @param stringCollection collection of strings to combine
     * @return the combined string
     */
    public static String combine(String separator, Collection<String> stringCollection) {
        return combineArray(separator, stringCollection.toArray(EMPTY_STRING_ARRAY));
    }

    /**
     * Combines a collection of strings into a single string, separated by the given character set
     *
     * @param startIndex       index to begin the separation at, inclusive
     * @param separator        character set included between each part of the given array
     * @param stringCollection collection of strings to combine
     * @return the combined string
     */
    public static String combine(int startIndex, String separator, Collection<String> stringCollection) {
        return combineArray(startIndex, separator, stringCollection.toArray(EMPTY_STRING_ARRAY));
    }

    /**
     * Combines a collection of strings into a single string, separated by the given character set
     *
     * @param startIndex       index to begin the separation at, inclusive
     * @param endIndex         index to end the separation at, exclusive
     * @param separator        character set included between each part of the given array
     * @param stringCollection collection of strings to combine
     * @return the combined string
     */
    public static String combine(int startIndex, int endIndex, String separator,
            Collection<String> stringCollection) {
        return combineArray(startIndex, endIndex, separator, stringCollection.toArray(EMPTY_STRING_ARRAY));
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(String separator, String... stringArray) {
        return combineArray(0, separator, stringArray);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param startIndex  index to begin the separation at, inclusive
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(int startIndex, String separator, String... stringArray) {
        return combineArray(startIndex, stringArray.length, separator, stringArray);
    }

    /**
     * Combines a set of strings into a single string, separated by the given character set
     *
     * @param startIndex  index to begin the separation at, inclusive
     * @param endIndex    index to end the separation at, exclusive
     * @param separator   character set included between each part of the given array
     * @param stringArray array to combine
     * @return the combined string
     */
    public static String combineArray(int startIndex, int endIndex, String separator, String... stringArray) {
        if (stringArray == null || startIndex >= endIndex) {
            return "";
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = startIndex; i < endIndex; i++) {
                builder.append(stringArray[i]);
                builder.append(separator);
            }
            builder.delete(builder.length() - separator.length(), builder.length());
            return builder.toString();
        }
    }
}

Related

  1. combine(Collection first, Collection second)
  2. combine(Collection... collections)
  3. combine(Collection strings)
  4. combine(final Collection[] c)
  5. combine(String separator, Collection parts)
  6. combine(String separator, Collection objs)
  7. combineAndRemoveDuplicates(Collection collection1, Collection collection2)
  8. combineCollections(Collection sessionCollection, Collection reqCollection)
  9. CombineCollections(final Collection... collections)