Java Iterable Join join(Iterable values, String separator)

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

Description

join

License

Apache License

Declaration

public static <T> String join(Iterable<T> values, String separator) 

Method Source Code

//package com.java2s;
/**//from  w w  w .j av a 2  s  .co m
 * Copyright 2010 Bing Ran<bing_ran@hotmail.com> 
 * 
 * Licensed 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 http://www.apache.org/licenses/LICENSE-2.0.
 * 
 * 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.Arrays;
import java.util.Iterator;

public class Main {
    public static <T> String join(Iterable<T> values, String separator) {
        if (values == null) {
            return "";
        }
        Iterator<T> iter = values.iterator();
        if (!iter.hasNext()) {
            return "";
        }
        StringBuffer toReturn = new StringBuffer(String.valueOf(iter.next()));
        while (iter.hasNext()) {
            toReturn.append(separator + String.valueOf(iter.next()));
        }
        return toReturn.toString();
    }

    public static String join(String[] values, String separator) {
        return (values == null) ? "" : join(Arrays.asList(values), separator);
    }
}

Related

  1. join(Iterable items, String delimiter)
  2. join(Iterable iterable)
  3. join(Iterable parts, String delimiter)
  4. join(Iterable sequence, String delimiter)
  5. join(Iterable values, String separator)
  6. join(Iterator iterator, String separator)
  7. join(String delimiter, Iterable strings)
  8. join(String delimiter, Iterable stringsIterable)
  9. join(String delimiter, String wrap, Iterable objs)