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

Open Source License

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: Open Source License 

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**// w  w  w.  j  a va 2s .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<Integer>}
     * separated by "\n":
     * <br>
     * <code>
     * List<Integer> list = new ArrayList<Integer>();
     * <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;
    }
}

Related

  1. join(Collection collection, String delimiter)
  2. join(Collection collection, String delimiter)
  3. join(Collection collection, String inputSeparator)
  4. join(Collection collection, String join)
  5. join(Collection collection, String sep)
  6. join(Collection collection, String separator)
  7. join(Collection collection, String separator)
  8. join(Collection collection, String separator)
  9. join(Collection collection, String separatorString)