Java Iterator Join join(String delimiter, Iterator iterator)

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

Description

join

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;

import java.util.Iterator;

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

    public static <T> String join(String delimiter, T... values) {
        if (delimiter == null) {
            delimiter = EMPTY_STRING;//from  www  . ja  va 2 s .co m
        }
        if (values.length == 0) {
            return EMPTY_STRING;
        } else if (values.length == 1) {
            return String.valueOf(values[0]);
        } else if (values.length == 2) {
            return String.valueOf(values[0]) + delimiter
                    + String.valueOf(values[1]);
        }
        StringBuilder joined = new StringBuilder();
        joined.append(String.valueOf(values[0]));
        for (int i = 1; i < values.length; i++) {
            joined.append(delimiter).append(values[i]);
        }
        return joined.toString();
    }

    public static String join(String delimiter, Iterator<?> iterator) {
        if (delimiter == null) {
            delimiter = EMPTY_STRING;
        }
        if (!iterator.hasNext()) {
            return EMPTY_STRING;
        }
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return String.valueOf(first);
        }
        StringBuilder joined = new StringBuilder();
        joined.append(first);
        while (iterator.hasNext()) {
            joined.append(delimiter).append(iterator.next());
        }
        return joined.toString();
    }
}

Related

  1. join(Iterator iterator, String separator)
  2. join(Iterator iterator, String separator)
  3. join(Iterator objects, String separator)
  4. join(Iterator iterator, String separator)
  5. join(Iterator iterator)
  6. join(String seperator, Iterator objects)
  7. join(String seperator, Iterator objects)
  8. joinIterators(final Iterator... iterators)