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

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

Description

Join elements of any collection with delimiter

License

Open Source License

Parameter

Parameter Description
T a parameter
objs a parameter
delimiter a parameter

Return

a joined string

Declaration

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

Method Source Code

//package com.java2s;
/*/* w w  w  . jav a2  s. co  m*/
 * CDDL HEADER START
 * 
 * The contents of this file are subject to the terms of the Common Development and Distribution
 * License, Version 1.0 only (the "License"). You may not use this file except in compliance with
 * the License.
 * 
 * You can obtain a copy of the license at license/ESCIDOC.LICENSE or
 * http://www.escidoc.org/license. See the License for the specific language governing permissions
 * and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL HEADER in each file and include the License
 * file at license/ESCIDOC.LICENSE. If applicable, add the following below this CDDL HEADER, with
 * the fields enclosed by brackets "[]" replaced with your own identifying information: Portions
 * Copyright [yyyy] [name of copyright owner]
 * 
 * CDDL HEADER END
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Join elements of any collection with delimiter
     * 
     * @param <T>
     * @param objs
     * @param delimiter
     * @return a joined string
     */
    public static <T> String join(final Collection<T> objs, String delimiter) {
        if (objs == null || objs.isEmpty())
            return "";
        if (!checkVal(delimiter))
            delimiter = "";
        Iterator<T> iter = objs.iterator();
        StringBuffer buffer = new StringBuffer();
        while (iter.hasNext()) {
            Object o = iter.next();
            String str = "";
            if (o != null) {
                str = o.toString();
                // empty strings will be omitted!
                if (checkVal(str))
                    buffer.append(str).append(delimiter);
            }
        }
        String result = buffer.toString();
        result = result.substring(0, result.length() - delimiter.length());
        return result;
    }

    /**
     * Returns true if val is not null && not empty String
     * 
     * @param val
     * @return first not null && not empty String
     */
    public static boolean checkVal(String val) {
        return (val != null && !val.trim().equals(""));
    }
}

Related

  1. join(final Collection values)
  2. join(final Collection values, final char separator)
  3. join(final Collection collection)
  4. join(final Collection collection, final String delimiter)
  5. join(final Collection objs, final String delimiter)
  6. join(final String d, final Collection c)
  7. join(final String delimiter, final Collection elements)
  8. join(final String s, final Collection c)
  9. join(final String sep, final Collection c)