Java Iterator Join join(String seperator, Iterator objects)

Here you can find the source of join(String seperator, Iterator objects)

Description

join

License

Apache License

Declaration

public static String join(String seperator, Iterator objects) 

Method Source Code


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

import java.util.Iterator;

public class Main {
    public static String join(String seperator, Iterator objects) {
        StringBuffer sb = new StringBuffer();
        if (objects.hasNext())
            sb.append(objects.next());//from   w  w  w . j  a v  a 2 s. co  m
        while (objects.hasNext()) {
            sb.append(seperator).append(objects.next());
        }
        return sb.toString();
    }

    public static String join(String seperator, Object[] objects) {
        int len = objects.length;
        StringBuffer sb = new StringBuffer(len * 2);
        if (len > 0) {
            sb.append(objects[0]);
            for (int i = 1; i < len; i++) {
                sb.append(seperator).append(objects[i]);
            }
        }
        return sb.toString();
    }

    public static String toString(Object[] objects) {
        return join(", ", objects);
    }
}

Related

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