Java Collection Join join(Collection collection, String separator)

Here you can find the source of join(Collection collection, String separator)

Description

join

License

Open Source License

Declaration

public static String join(Collection<String> collection, String separator) 

Method Source Code

//package com.java2s;
/*//from   ww  w  .j av  a  2 s  . co  m
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * 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;

public class Main {
    public static String join(String[] array, String delimiter, int firstIndex, int count) {
        switch (count) {
        case 0:
            return "";
        case 1:
            return array[firstIndex];
        case 2:
            return array[firstIndex] + delimiter + array[firstIndex + 1];
        case 3:
            return array[firstIndex] + delimiter + array[firstIndex + 1] + delimiter + array[firstIndex + 2];
        case 4:
            return array[firstIndex] + delimiter + array[firstIndex + 1] + delimiter + array[firstIndex + 2]
                    + delimiter + array[firstIndex + 3];
        case 5:
            return array[firstIndex] + delimiter + array[firstIndex + 1] + delimiter + array[firstIndex + 2]
                    + delimiter + array[firstIndex + 3] + delimiter + array[firstIndex + 4];
        default:
            StringBuilder sb = new StringBuilder(array[0]);
            for (int i = firstIndex + 1; i < count; i++) {
                sb.append(delimiter);
                sb.append(array[i]);
            }

            return sb.toString();
        }
    }

    public static String join(Collection<String> collection, String separator) {
        if (collection == null)
            return null;
        if (collection.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
        for (String s : collection) {
            if (sb.length() != 0)
                sb.append(separator);
            sb.append(s);
        }
        return sb.toString();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Related

  1. join(Collection coll, String delimiter)
  2. join(Collection collection)
  3. join(Collection collection)
  4. join(Collection collection, String separator)
  5. join(Collection collection, String separator)
  6. join(Collection collection, String separator)
  7. join(Collection elements)
  8. join(Collection in, String joinS)
  9. join(Collection items)