Java Collection Join join(Collection items)

Here you can find the source of join(Collection items)

Description

join

License

Apache License

Declaration

public static String join(Collection<String> items) 

Method Source Code

//package com.java2s;
/**/*from  w w  w . j  av  a2  s. c  o  m*/
Copyright 2014 Pieter Rautenbach
    
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.Collection;
import java.util.Iterator;

public class Main {
    public static String join(Collection<String> items) {
        String separator = ", ";
        Iterator<String> iterator = items.iterator();
        StringBuilder stringBuilder = new StringBuilder();
        if (iterator.hasNext()) {
            stringBuilder.append(iterator.next());
            while (iterator.hasNext()) {
                stringBuilder.append(separator).append(iterator.next());
            }
        }

        return stringBuilder.toString();
    }
}

Related

  1. join(Collection collection, String separator)
  2. join(Collection collection, String separator)
  3. join(Collection collection, String separator)
  4. join(Collection elements)
  5. join(Collection in, String joinS)
  6. join(Collection items, String join)
  7. join(Collection items, String prefix, String suffix, String itemPrefix, String itemSuffix)
  8. join(Collection list)
  9. join(Collection list, String conjunction)