Java Iterable Join join(Iterable coll, String separator)

Here you can find the source of join(Iterable coll, String separator)

Description

Joins into a string an iterable collection of objects by using a separator.

License

Open Source License

Parameter

Parameter Description
coll the collection of elements to be converted in strings and joined together
separator the separator to put in between any two adjacent strings

Declaration

public static <T> String join(Iterable<T> coll, String separator) 

Method Source Code


//package com.java2s;
/*//from  w  w  w .  j  a  v  a2 s  . co m
 * Copyright (C) 2014 Riccardo Traverso
 * 
 * This file is part of JavaUtils
 * Website: https://github.com/rtraverso86/JavaUtils
 * 
 * JavaUtils is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JavaUtils is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Iterator;

public class Main {
    /**
     * Joins into a string an array of objects by using a separator.
     * 
     * @param arr
     *            the array of elements to be converted in strings and joined
     *            together
     * @param separator
     *            the separator to put in between any two adjacent strings
     */
    public static String join(Object[] arr, String separator) {
        if (arr == null || arr.length < 1)
            return "";
        StringBuilder sb = new StringBuilder(arr[0].toString());
        for (int i = 1; i < arr.length; ++i) {
            if (!isBlank(separator)) {
                sb.append(separator);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    /**
     * Joins into a string an iterable collection of objects by using a
     * separator.
     * 
     * @param coll
     *            the collection of elements to be converted in strings and
     *            joined together
     * @param separator
     *            the separator to put in between any two adjacent strings*
     */
    public static <T> String join(Iterable<T> coll, String separator) {
        if (coll == null)
            return "";
        Iterator<T> it = coll.iterator();
        if (!it.hasNext())
            return "";
        StringBuilder sb = new StringBuilder(it.next().toString());
        while (it.hasNext()) {
            if (!isBlank(separator)) {
                sb.append(separator);
            }
            sb.append(it.next());
        }
        return sb.toString();
    }

    /**
     * Returns true iff s is either null or the empty string.
     * 
     * @param s the string to be tested
     */
    public static boolean isBlank(String s) {
        return s == null || s.isEmpty();
    }
}

Related

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