Java Iterable Join join(final Iterable objs, final String delimiter)

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

Description

Source: http://snippets.dzone.com/posts/show/91

License

Open Source License

Parameter

Parameter Description
T a parameter
objs a parameter
delimiter a parameter

Return

String representations of the Iterable object's elements joined together with the given delimiter

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Dipanjan Das //w w  w .  j  a  v a2 s .  com
 * Language Technologies Institute, 
 * Carnegie Mellon University, 
 * All Rights Reserved.
 * 
 * StringUtil.java is part of SEMAFOR 2.0.
 * 
 * SEMAFOR 2.0 is free software: you can redistribute it and/or modify  it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * SEMAFOR 2.0 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 General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License along
 * with SEMAFOR 2.0.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.util.Iterator;

public class Main {
    /**
     * Source: http://snippets.dzone.com/posts/show/91
     * @param <T>
     * @param objs
     * @param delimiter
     * @return String representations of the Iterable object's elements joined together with the given delimiter
     */
    public static <T> String join(final Iterable<T> objs,
            final String delimiter) {
        Iterator<T> iter = objs.iterator();
        if (!iter.hasNext())
            return "";
        StringBuffer buffer = new StringBuffer(String.valueOf(iter.next()));
        while (iter.hasNext())
            buffer.append(delimiter).append(String.valueOf(iter.next()));
        return buffer.toString();
    }

    public static String join(Object[] items, String delimiter) {
        StringBuffer buffer = new StringBuffer("");
        boolean first = true;
        for (Object item : items) {
            if (!first)
                buffer.append(delimiter);
            buffer.append(String.valueOf(item));
            first = false;
        }
        return buffer.toString();
    }
}

Related

  1. join(final Iterable... iterables)
  2. join(final Iterable items, final String separator)
  3. join(final Iterable iterable, final String separator)
  4. join(final Iterable container)
  5. join(final Iterable iterable, final String joinString)
  6. join(final Iterable objs, final String delimiter)
  7. join(final Iterable source)
  8. join(final Iterable... iterables)
  9. join(final String delimiter, final Iterable objs)