Java Iterable Join join(Iterable elements, String separator)

Here you can find the source of join(Iterable elements, String separator)

Description

join

Declaration

public static String join(Iterable<?> elements, String separator) 

Method Source Code

//package com.java2s;

import java.util.Iterator;

public class Main {
    public static String join(String... parts) {
        StringBuilder sb = new StringBuilder(parts.length);
        for (String part : parts) {
            sb.append(part);//from   ww w  .ja  va2 s . c  o  m
        }
        return sb.toString();
    }

    public static String join(Iterable<?> elements, String separator) {
        if (elements == null) {
            return "";
        }
        return join(elements.iterator(), separator);
    }

    public static String join(Iterator<?> elements, String separator) {
        if (elements == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        while (elements.hasNext()) {
            Object o = elements.next();
            if (sb.length() > 0 && separator != null) {
                sb.append(separator);
            }
            sb.append(o);
        }
        return sb.toString();
    }

    public static String join(Object[] elements, String separator) {
        if (elements == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (Object o : elements) {
            if (sb.length() > 0 && separator != null) {
                sb.append(separator);
            }
            sb.append(o);
        }
        return sb.toString();
    }
}

Related

  1. join(Iterable iterable, String delimiter)
  2. join(Iterable joinee, String joiner)
  3. join(Iterable c, String delimeter)
  4. join(Iterable elements, String separator)
  5. join(Iterable elements, String separator)
  6. join(Iterable items)
  7. join(Iterable items)
  8. join(Iterable items, String delimiter)
  9. join(Iterable items, String separator)