Java Collection Join join(String glue, Collection items)

Here you can find the source of join(String glue, Collection items)

Description

Produces a string that consists of the toString of each element in collection with the glue string separating each item

License

Open Source License

Parameter

Parameter Description
T type of object to join
glue the string that will be inserted between elements of items
items the elements to join into a string

Return

a string containing all elements of items separated by glue

Declaration

public static <T> String join(String glue, Collection<T> items) 

Method Source Code

//package com.java2s;

import java.util.Collection;

public class Main {
    /**/*from ww  w .j a va  2  s . c o m*/
     * Produces a string that consists of the <code>toString</code> of each element in collection with the glue string separating each item
     * @param <T> type of object to join
     * @param glue the string that will be inserted between elements of items
     * @param items the elements to join into a string
     * @return a string containing all elements of items separated by glue
     */
    public static <T> String join(String glue, Collection<T> items) {
        final StringBuilder sb = new StringBuilder();
        String join = "";
        for (final T item : items) {
            sb.append(join);
            join = glue;
            sb.append(item.toString());
        }
        return sb.toString();
    }
}

Related

  1. join(String delimiter, Collection values)
  2. join(String glue, Collection c)
  3. join(String glue, Collection pieces)
  4. join(String glue, Collection c)
  5. join(String glue, Collection strings)
  6. join(String join_string, Collection c)
  7. join(String joiner, Collection strings)
  8. join(String sep, Collection objects)
  9. join(String sep, Collection col)