Java Iterable Join join(Iterable iterable, String separator)

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

Description

join

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
/*//from  w  ww.  j a  va2 s.co  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Iterator;

public class Main {
    public static String join(Iterable<?> iterable, String separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return first == null ? "" : first.toString();
            } else {
                StringBuilder buf = new StringBuilder(256);
                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();
            }
        }
    }
}

Related

  1. join(Iterable elements, String separator)
  2. join(Iterable items)
  3. join(Iterable items)
  4. join(Iterable items, String delimiter)
  5. join(Iterable items, String separator)
  6. join(Iterable iterable, String separator)
  7. join(Iterable list, String delimiter)
  8. join(Iterable objects)
  9. join(Iterable objects, String delimiter)