Java Collection Join join(Collection c, String delim)

Here you can find the source of join(Collection c, String delim)

Description

Similar to perl's join() method on lists, but works with all collections.

License

Open Source License

Parameter

Parameter Description
c An iterable collection of Objects
delim a string to put between each object

Return

a joined toString() representation of the collection

Declaration

public static String join(Collection<?> c, String delim) 

Method Source Code


//package com.java2s;
/*/*from w  w  w. jav a2 s  .com*/
 * TextUtilities.java - Various text functions
 * Copyright (C) 1998, 2005 Slava Pestov
 * :tabSize=4:indentSize=4:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * This program 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 2
 * of the License, or any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.*;

public class Main {
    /** Similar to perl's join() method on lists,
     *    but works with all collections.
     *
     * @param c An iterable collection of Objects
     * @param delim a string to put between each object
     * @return a joined toString() representation of the collection
     *
     * @since jedit 4.3pre3
     */
    public static String join(Collection<?> c, String delim) {
        StringBuilder retval = new StringBuilder();
        Iterator<?> itr = c.iterator();
        if (itr.hasNext())
            retval.append(itr.next());
        else
            return "";
        while (itr.hasNext()) {
            retval.append(delim);
            retval.append(itr.next());
        }
        return retval.toString();
    }
}

Related

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