Java Collection Join join(Collection collection, String separator)

Here you can find the source of join(Collection collection, String separator)

Description

Takes a collection and returns all elements assembled in a String joined by the defined separator.

License

LGPL

Parameter

Parameter Description
collection a Collection of objects to join.
separator the String separator used to join the collection.

Return

joined string.

Declaration

public static String join(Collection<?> collection, String separator) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**//from w w  w . j  av a2  s.c o  m
     * Takes a collection and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using a {@link List}
     * separated by "\n":
     * <br>
     * <code>
     * List list = new ArrayList();
     * <br>
     * list.add(1);
     * <br>
     * list.add(2);
     * <br>
     * list.add(3);
     * <br>
     * String output = CollectionUtils.join(list, "\n");
     * <br>
     * </code>
     * 
     * @param collection a {@link Collection} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(Collection<?> collection, String separator) {
        String output = "";
        if (collection != null) {
            Iterator<?> iterator = collection.iterator();
            while (iterator.hasNext()) {
                Object o = iterator.next();
                output += o;
                if (iterator.hasNext())
                    output += separator;
            }
        }
        return output;
    }

    /**
     * Takes an array of Objects and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link Integer[]}
     * separated by "\n":
     * <br>
     * <code>
     * Integer[] array = {1, 2, 3};
     * <br>
     * String output = CollectionUtils.join(array, "\n");
     * <br>
     * </code>
     * 
     * @param collection a {@link Collection} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(Object[] collection, String separator) {
        String output = "";
        if (collection != null) {
            for (int i = 0; i < collection.length - 1; i++) {
                Object o = collection[i];
                output += o;
                output += separator;
            }
            if (collection.length > 0)
                output += collection[collection.length - 1];
        }
        return output;
    }

    /**
     * Takes an array of booleans and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link boolean[]}
     * separated by ", ":
     * <br>
     * <code>
     * boolean[] array = {true, true, false};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link boolean[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(boolean[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Boolean.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Boolean.toString(collection[collection.length - 1]);
        return output;
    }

    /**
     * Takes an array of chars and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link char[]}
     * separated by ", ":
     * <br>
     * <code>
     * boolean[] array = {true, true, false};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link boolean[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(char[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Character.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Character.toString(collection[collection.length - 1]);
        return output;
    }

    /**
     * Takes an array of doubles and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link double[]}
     * separated by ", ":
     * <br>
     * <code>
     * double[] array = {1.0, 2.0, 3.0};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link double[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(double[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Double.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Double.toString(collection[collection.length - 1]);
        return output;

    }

    /**
     * Takes an array of primitive integer and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link int[]}
     * separated by ", ":
     * <br>
     * <code>
     * int[] array = {1, 2, 3};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link int[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(int[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Integer.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Integer.toString(collection[collection.length - 1]);
        return output;
    }
}

Related

  1. join(Collection collection, String join)
  2. join(Collection collection, String sep)
  3. join(Collection collection, String separator)
  4. join(Collection collection, String separator)
  5. join(Collection collection, String separator)
  6. join(Collection collection, String separatorString)
  7. join(Collection elements, String separator)
  8. join(Collection items, char separator)
  9. join(Collection items, String delim)