Java Collection Join join(String sep, Collection values)

Here you can find the source of join(String sep, Collection values)

Description

Join collection into a single string, with a separator between.

License

Open Source License

Declaration

public static String join(String sep, Collection<?> values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/*from  w w  w  . jav  a 2 s  . co m*/
     * Join collection into a single string, with a separator between.
     */
    public static String join(String sep, Collection<?> values) {
        StringBuilder builder = new StringBuilder();
        join(builder, sep, values);
        return builder.toString();
    }

    /**
     * Join collection into and append to a StringBuilder, with a separator between.
     * Useful if you want to join strings and append to an existing StringBuilder.
     * @param builder StringBuilder you want to append to. This variable will be modified.
     * @param sep Separator between strings
     * @param values collection of strings to join.
     */
    public static void join(StringBuilder builder, String sep,
            Collection<?> values) {
        boolean first = true;
        for (Object o : values) {
            if (first)
                first = false;
            else
                builder.append(sep);
            builder.append("" + o);
        }
    }

    /**
     * Join an multi value list into a single string, with a separator between.
     */
    public static <T> void join(StringBuilder builder, String sep,
            T... values) {
        boolean first = true;
        for (Object o : values) {
            if (first)
                first = false;
            else
                builder.append(sep);
            builder.append("" + o);
        }
    }

    /**
     * Join an multi value list into a single string, with a separator between.
     */
    public static <T> String join(String sep, T... values) {
        StringBuilder builder = new StringBuilder();
        join(builder, sep, values);
        return builder.toString();
    }
}

Related

  1. join(String sep, Collection objects)
  2. join(String sep, Collection col)
  3. join(String sep, Collection col)
  4. join(String sep, Collection parts)
  5. join(String sep, Collection values)
  6. join(String sep, Collection strings)
  7. join(String sep, Collection strs)
  8. join(String separator, Collection c)
  9. join(String separator, Collection c)