Java Collection Join join(Collection objectsToJoin, String separator)

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

Description

Joins all the strings in the list in a single one separated by the separator sequence.

License

Apache License

Parameter

Parameter Description
objectsToJoin The objects to join.
separator The separator sequence.

Return

The joined strings.

Declaration

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

Method Source Code


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

import java.util.Collection;

public class Main {
    public final static String EMPTY = "";
    public final static String COMMA = ",";

    /**/*ww  w.  ja va2  s.c  o  m*/
     * Joins all the strings in the list in a single one separated by the separator sequence.
     * 
     * @param objectsToJoin The objects to join.
     * @param separator The separator sequence.
     * @return The joined strings.
     */
    public static String join(Collection<?> objectsToJoin, String separator) {
        if ((objectsToJoin != null) && !objectsToJoin.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            for (Object object : objectsToJoin) {
                builder.append(object);
                builder.append(separator);
            }
            // Remove the last separator
            return builder.substring(0, builder.length() - separator.length());
        } else {
            return EMPTY;
        }
    }

    /**
     * Joins all the strings in the list in a single one separated by ','.
     * 
     * @param objectsToJoin The objects to join.
     * @return The joined strings.
     */
    public static String join(Collection<?> objectsToJoin) {
        return join(objectsToJoin, COMMA);
    }

    public static Boolean isEmpty(String text) {
        return text != null ? text.length() == 0 : true;
    }
}

Related

  1. join(Collection items, char separator)
  2. join(Collection items, String delim)
  3. join(Collection items, String delimiter)
  4. join(Collection iterable, String delimiter)
  5. join(Collection objects, String token, StringBuilder buf)
  6. join(Collection objs, String delim)
  7. join(Collection objs, String delim)
  8. join(Collection objs, String delimiter)
  9. join(Collection objs, String sep)