Java Collection Join join(Collection strings, String separator)

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

Description

Joins the elements of the provided Collection collection into a single String string , using the provided separator and string elements.

License

Apache License

Parameter

Parameter Description
strings Object objects to stringify and join together
separator Separator String string

Return

String

Declaration

public static String join(Collection<? extends Object> strings, String separator) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from   w w w  .j av  a  2 s  .  co  m*/
     * Joins the elements of the provided {@link Collection collection} into a
     * single {@link String string}, using the provided {@code separator} and
     * string elements.
     * <p>
     * For example: <br>
     * <blockquote> {@code join(asList("foo", "bar"), "*")}<br>
     * </blockquote> returns<br>
     * <blockquote> {@code "foo*bar"} </blockquote>
     * </p>
     *
     * @param strings {@link Object objects} to stringify and join together
     * @param separator Separator {@link String string}
     * @return String
     */
    public static String join(Collection<? extends Object> strings, String separator) {
        StringBuilder sb = new StringBuilder();
        if (strings != null) {
            Iterator<? extends Object> i = strings.iterator();
            while (i.hasNext()) {
                sb.append(i.next());
                if (i.hasNext()) {
                    sb.append(separator);
                }
            }
        }
        return sb.toString();
    }
}

Related

  1. join(Collection collection, String joinString)
  2. join(Collection collection, String separator)
  3. join(Collection elements, String separator)
  4. join(Collection list, String separator)
  5. join(Collection objects, String delim)
  6. join(Collection strs, String separator)
  7. join(Collection c, String delim)
  8. join(Collection c, String delimiter)
  9. join(Collection c, String insert)