Java Collection Join join(Collection collection, String joinString)

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

Description

Join a collection of items into a string.

License

Open Source License

Parameter

Parameter Description
collection A collection of items to join
joinString The string to place between the items

Return

A string consisting of the string representations of the items in collection, concatenated, with joinString interspersed between them.

Declaration

public static <T> String join(Collection<T> collection, String joinString) 

Method Source Code


//package com.java2s;

import java.util.Collection;

public class Main {
    /**/* w w w  . ja  va2s.  co  m*/
     * Join a collection of items into a string.
     * @param collection A collection of items to join
     * @param joinString The string to place between the items
     * @return A string consisting of the string representations of the items in
     *         collection, concatenated, with joinString interspersed between them.
     */
    public static <T> String join(Collection<T> collection, String joinString) {

        StringBuilder result = new StringBuilder();

        boolean isFirst = true;
        for (final T elem : collection) {
            if (!isFirst) {
                result.append(joinString);
            } else {
                isFirst = false;
            }

            result.append(elem.toString());
        }

        return result.toString();
    }

    /**
     * Convert the given value to a string value, or simply return an empty string
     * if the specified value is <code>null</code>.
     * @param value
     * @return String
     */
    public static String toString(Object value) {
        return value == null ? "" : value.toString();
    }

    /**
     * Convert the given value to a string value, or simply return the default string
     * if the specified value is <code>null</code>.
     * @param value
     * @param defaultString 
     * @return String
     */
    public static String toString(Object value, String defaultString) {
        return value == null ? defaultString : value.toString();
    }
}

Related

  1. join(Collection words, String character)
  2. join(Collection c, String concatinator)
  3. join(Collection coll, String separator)
  4. join(Collection collec, String delimiter)
  5. join(Collection collection, CharSequence joinedBy)
  6. join(Collection collections, String separator)
  7. join(Collection list, final String separator)
  8. join(Collection list, String delimiter)
  9. join(Collection s, String delimiter)