Java Collection Join join(String separator, Collection values)

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

Description

join

License

MIT License

Declaration

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

Method Source Code

//package com.java2s;
/*!// www  .  j  a  va 2 s.  c  o m
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

import java.util.Collection;

public class Main {
    public static String join(String separator, Object... values) {
        return join(new StringBuilder(), separator, values).toString();
    }

    public static StringBuilder join(StringBuilder sb, String separator, Object... values) {
        if (values == null || values.length == 0) {
            return sb;
        }

        boolean isFirst = true;
        for (Object value : values) {
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(separator);
            }
            if (value != null) {
                sb.append(value.toString());
            }
        }

        return sb;
    }

    public static <T> String join(String separator, Collection<T> values) {
        return join(new StringBuilder(), separator, values).toString();
    }

    public static <T> StringBuilder join(StringBuilder sb, String separator, Collection<T> values) {
        if (values == null || values.isEmpty()) {
            return sb;
        }
        return join(sb, separator, values.toArray(new Object[values.size()]));
    }

    public static String toString(StringBuilder sb, boolean trim) {
        if (sb == null) {
            return null;
        }

        if (!trim) {
            return sb.toString();
        }

        int len = sb.length();
        if (len == 0) {
            return "";
        }

        int sIdx;
        for (sIdx = 0; sIdx < len; sIdx++) {
            char ch = sb.charAt(sIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        if (sIdx == len) {
            return "";
        }

        int eIdx;
        for (eIdx = len - 1; 0 <= eIdx; eIdx--) {
            char ch = sb.charAt(eIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        return sb.substring(sIdx, eIdx + 1);
    }
}

Related

  1. join(String separator, Collection objs)
  2. join(String separator, Collection list)
  3. join(String separator, Collection parts)
  4. join(String separator, Collection strings)
  5. join(String separator, Collection objects)
  6. join(String token, Collection strings)
  7. join(String[] collection, String delimiter)
  8. join(String[] collection, String separator)
  9. join(StringBuilder builder, Collection strs)