Java Iterator Join join(Iterator iterator, String separator)

Here you can find the source of join(Iterator iterator, String separator)

Description

Joins a iteratable list of objects by the given separator

License

Apache License

Parameter

Parameter Description
iterator the list iterator (objects .toString method will be used for the element representation)
separator the string which should separate the elements

Return

a new string of the objects

Declaration

public static String join(Iterator<?> iterator, String separator) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;
import java.util.List;

public class Main {
    /**//  w w  w  .j  av a2s. co  m
     * Joins a iteratable list of objects by the given separator
     *
     * @param iterator
     *            the list iterator (objects .toString method will be used for the element representation)
     * @param separator
     *            the string which should separate the elements
     * @return a new string of the objects
     */
    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null)
            return "";
        // handle null, zero and one elements before building a buffer
        Object first = iterator.next();
        if (!iterator.hasNext())
            return first.toString();
        // two or more elements
        StringBuilder buf = new StringBuilder();
        if (first != null) {
            buf.append(first);
        }
        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    /**
     * Joins a ArrayList of Strings with the given separator
     *
     * @param list
     *            the ArrayList of Strings to join
     * @param separator
     *            a separator string
     * @return a new string representation of the list
     */
    public static String join(List<String> list, String separator) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String entry : list) {
            if (i != 0) {
                sb.append(separator);
            }
            sb.append(entry);
            i++;
        }
        return sb.toString();
    }

    /**
     * Joins a string array with the given separator
     *
     * @param list
     *            the String array of items to join
     * @param separator
     *            the separator string
     * @return a new string representation of the array
     */
    public static String join(String[] list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.length; i++) {
            if (i != 0) {
                sb.append(separator);
            }
            sb.append(list[i]);
        }
        return sb.toString();
    }
}

Related

  1. join(Iterator iterator, String s)
  2. join(Iterator iterator, String separator)
  3. join(Iterator iterator, String separator)
  4. join(Iterator objects, String separator)
  5. join(Iterator iterator)
  6. join(String delimiter, Iterator iterator)
  7. join(String seperator, Iterator objects)
  8. join(String seperator, Iterator objects)