Java Iterable Join join(Iterable sequence, String delimiter)

Here you can find the source of join(Iterable sequence, String delimiter)

Description

Join string representation of the elements of the given collection separated with the given delimiter.

License

Apache License

Parameter

Parameter Description
sequence The items to be converted into string.
delimiter The delimiter to be used between distinct items of the collection.

Return

String representations of the collection's items separated with the given delimiter.

Declaration

public static <T> String join(Iterable<T> sequence, String delimiter) 

Method Source Code

//package com.java2s;
/*/*from  w w w . ja va  2s .c  o  m*/
 * Entwined STM
 * 
 * (c) Copyright 2013 CERN. This software is distributed under the terms of the Apache License Version 2.0, copied
 * verbatim in the file "COPYING". In applying this licence, CERN does not waive the privileges and immunities granted
 * to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction.
 */

import java.util.Iterator;

public class Main {
    /**
     * Join string representation of the elements of the given collection separated with the given delimiter.
     * 
     * @param sequence The items to be converted into string.
     * @param delimiter The delimiter to be used between distinct items of the collection.
     * @return String representations of the collection's items separated with the given delimiter.
     */
    public static <T> String join(Iterable<T> sequence, String delimiter) {
        if (null == sequence) {
            return "null";
        }
        if (null == delimiter) {
            delimiter = "";
        }
        Iterator<T> iterator = sequence.iterator();
        if (!iterator.hasNext()) {
            return "";
        }
        StringBuffer buffer = new StringBuffer(256);
        buffer.append(iterator.next());
        while (iterator.hasNext()) {
            buffer.append(delimiter);
            buffer.append(iterator.next());
        }
        return buffer.toString();
    }
}

Related

  1. join(Iterable coll, String sep)
  2. join(Iterable coll, String separator)
  3. join(Iterable items, String delimiter)
  4. join(Iterable iterable)
  5. join(Iterable parts, String delimiter)
  6. join(Iterable values, String separator)
  7. join(Iterable values, String separator)
  8. join(Iterator iterator, String separator)
  9. join(String delimiter, Iterable strings)