Java Collection Join join(Collection c, String delim)

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

Description

join

License

Open Source License

Declaration

public static String join(Collection c, String delim) 

Method Source Code

//package com.java2s;
/*//from w  w w .j ava2  s  .  c  o m
StringUtils.java : A stand alone utility class.
Copyright (C) 2000 Justin P. McCarthy
    
This library 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 2.1 of the License, or (at your option) any later version.
    
This library 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 library; if not, see <http://www.gnu.org/licenses/>.
    
To further contact the author please email jpmccar@gjt.org
    
    
Modifications, copyright 2001-2014 Tuma Solutions, LLC; distributed under the
LGPL, as described above.
    
*/

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(Collection c, String delim) {
        if (c == null || c.isEmpty())
            return "";
        else if (c.size() == 1)
            return String.valueOf(c.iterator().next());

        StringBuffer result = new StringBuffer();
        for (Iterator i = c.iterator(); i.hasNext();)
            result.append(delim).append(i.next());
        return result.substring(delim.length());
    }
}

Related

  1. join(AbstractCollection s, String delimiter)
  2. join(CharSequence p, Collection collection)
  3. join(CharSequence separator, Collection col)
  4. join(Collection c)
  5. join(Collection c, String d)
  6. join(Collection c, String left, String right, String separator)
  7. join(Collection c, String separator)
  8. join(Collection col, char sep)
  9. join(Collection coll, String separator)