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

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

Description

join

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*//from   w ww.  j  a  v a2 s. c o m
 * Copyright 2009 Loic BARRAULT.  
 * Portions Copyright BBN and UMD (see LICENSE_TERP.txt).  
 * Portions Copyright 1999-2008 CMU (see LICENSE_SPHINX4.txt).
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "LICENSE.txt" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

import java.util.Iterator;

public class Main {
    public static String join(String[] s, String delimiter) {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < s.length; ++i) {
            if (i > 0) {
                buffer.append(delimiter);
            }
            buffer.append(s[i]);
        }
        return buffer.toString();
    }

    public static String join(final String delimiter,
            final Comparable<String>[] objs) {
        if (objs == null)
            return "";
        if (objs.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(objs[0]));
        for (int i = 1; i < objs.length; i++) {
            buffer.append(delim).append(String.valueOf(objs[i]));
        }
        return buffer.toString();
    }

    public static <T> String join(final String delimiter,
            final Iterable<T> objs) {
        if (objs == null)
            return "";
        Iterator<T> iter = objs.iterator();
        if (!iter.hasNext())
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";
        StringBuilder buffer = new StringBuilder(
                String.valueOf(iter.next()));
        while (iter.hasNext())
            buffer.append(delim).append(String.valueOf(iter.next()));
        return buffer.toString();
    }

    public static String join(String delimiter, String format, double[] arr) {
        if (arr == null)
            return "";
        String delim = delimiter;
        if (delim == null)
            delim = "";
        StringBuilder buffer = new StringBuilder(String.format(format,
                arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.format(format, arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, double[] arr) {
        if (arr == null)
            return "";
        if (arr.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.valueOf(arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, Double[] arr) {
        if (arr == null)
            return "";
        if (arr.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.valueOf(arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, int[] arr) {
        if (arr == null)
            return "";
        if (arr.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.valueOf(arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, String format, float[] arr) {
        if (arr == null)
            return "";
        String delim = delimiter;
        if (delim == null)
            delim = "";
        StringBuilder buffer = new StringBuilder(String.format(format,
                arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.format(format, arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, float[] arr) {
        if (arr == null)
            return "";
        if (arr.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.valueOf(arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, String format, Float[] arr) {
        if (arr == null)
            return "";
        String delim = delimiter;
        if (delim == null)
            delim = "";
        StringBuilder buffer = new StringBuilder(String.format(format,
                arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.format(format, arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, Float[] arr) {
        if (arr == null)
            return "";
        if (arr.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.valueOf(arr[i]));
        }
        return buffer.toString();
    }

    public static String join(String delimiter, char[] arr) {
        if (arr == null)
            return "";
        if (arr.length == 0)
            return "";
        String delim = delimiter;
        if (delimiter == null)
            delim = "";

        StringBuilder buffer = new StringBuilder(String.valueOf(arr[0]));
        for (int i = 1; i < arr.length; i++) {
            buffer.append(delim).append(String.valueOf(arr[i]));
        }
        return buffer.toString();
    }
}

Related

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