Java Iterator Join join(Iterator iterator, String separator)

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

Description

join

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static String join(Iterator iterator, String separator) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    public static final String EMPTY = "";

    @SuppressWarnings("unchecked")
    public static String join(Iterator iterator, String separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }/*from  w w w .  j a  va  2s. c om*/
        if (!iterator.hasNext()) {
            return EMPTY;
        }
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first == null ? "" : first.toString();
        }

        // two or more elements
        StringBuilder buf = new StringBuilder(256); // Java default is 16,
        // probably too small
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            buf.append(separator);
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }

        return buf.toString();
    }
}

Related

  1. join(Iterator iterator, String s)
  2. join(Iterator iterator, String separator)
  3. join(Iterator objects, String separator)
  4. join(Iterator iterator, String separator)
  5. join(Iterator iterator)