Java Collection Join join(String separator, Collection c)

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

Description

join

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static String join(String separator, Collection c) 

Method Source Code

//package com.java2s;
/**//w  w  w  . jav  a  2s  . c  o  m
 * Tencent is pleased to support the open source community by making Tars available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * 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 {
    @SuppressWarnings("unchecked")
    public static String join(String separator, Collection c) {
        if (c.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
        Iterator i = c.iterator();
        sb.append(i.next());
        while (i.hasNext()) {
            sb.append(separator);
            sb.append(i.next());
        }
        return sb.toString();
    }

    public static String join(String separator, String[] s) {
        return joinArray(separator, s);
    }

    public static String join(String separator, Object... c) {
        return joinArray(separator, c);
    }

    public static boolean isEmpty(String s) {
        if (s == null)
            return true;
        return s.trim().isEmpty();
    }

    public static String joinArray(String separator, Object[] s) {
        if (s == null || s.length == 0)
            return "";
        StringBuilder sb = new StringBuilder();
        sb.append(s[0]);
        for (int i = 1; i < s.length; ++i) {
            if (s[i] != null) {
                sb.append(separator);
                sb.append(s[i].toString());
            }
        }
        return sb.toString();
    }

    public static String joinArray(String separator, int[] s) {
        if (s == null || s.length == 0)
            return "";
        StringBuilder sb = new StringBuilder();
        sb.append(s[0]);
        for (int i = 1; i < s.length; ++i) {
            sb.append(separator);
            sb.append(s[i]);
        }
        return sb.toString();
    }

    public static String trim(String s) {
        if (s == null)
            return null;
        return s.trim();
    }
}

Related

  1. join(String sep, Collection values)
  2. join(String sep, Collection values)
  3. join(String sep, Collection strings)
  4. join(String sep, Collection strs)
  5. join(String separator, Collection c)
  6. join(String separator, Collection items)
  7. join(String separator, Collection items)
  8. join(String separator, Collection objs)
  9. join(String separator, Collection list)