Java Collection Join join(Collection strings, String delimiter)

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

Description

Implements join

License

Open Source License

Parameter

Parameter Description
strings a parameter
delimiter a parameter

Declaration

public static String join(Collection<?> strings, String delimiter) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from   w ww.  j ava2s  . c  o  m*/
     * Implements join
     *
     * @param strings
     * @param delimiter
     * @return
     */
    public static String join(Collection<?> strings, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<?> iter = strings.iterator();
        if (iter.hasNext()) {
            buffer.append(iter.next().toString());
            while (iter.hasNext()) {
                buffer.append(delimiter);
                buffer.append(iter.next().toString());
            }
        }
        return buffer.toString();
    }

    /**
     * Join on an Array of Strings
     *
     * @param strings
     * @param delimiter
     * @return
     */
    public static String join(String[] strings, String delimiter) {
        if (strings != null && strings.length > 0) {
            Collection<Object> collection = new ArrayList<Object>();
            for (String str : strings) {
                collection.add(str);
            }
            return join(collection, delimiter);
        }
        return null;
    }

    /**
     * Join on an Array of objects
     *
     * @param strings
     * @param delimiter
     * @return
     */
    public static String join(Object[] strings, String delimiter) {
        if (strings != null && strings.length > 0) {
            Collection<Object> collection = new ArrayList<Object>();
            for (Object obj : strings) {
                collection.add(obj.toString());
            }
            return join(collection, delimiter);
        }
        return null;
    }
}

Related

  1. join(Collection objs, String sep)
  2. join(Collection s)
  3. join(Collection s, String delimiter)
  4. join(Collection s, String delimiter)
  5. join(Collection s, String delimiter)
  6. join(Collection strings, String delimiter)
  7. join(Collection strings, String separator)
  8. join(Collection strings, String separator)
  9. join(Collection strings, String separator)