Java Collection Join join(Collection tokens, String d)

Here you can find the source of join(Collection tokens, String d)

Description

Join the specified tokens into a delimited string.

License

Apache License

Parameter

Parameter Description
tokens The tokens to join.
d The delimiter.

Return

The delimited string. If tokens is null, returns null.

Declaration

public static String join(Collection<?> tokens, String d) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.util.*;

public class Main {
    /**/*from w w w  . ja  v a 2s .com*/
     * Join the specified tokens into a delimited string.
     *
     * @param tokens The tokens to join.
     * @param separator The delimiter.
     * @return The delimited string.  If <code>tokens</code> is <jk>null</jk>, returns <jk>null</jk>.
     */
    public static String join(Object[] tokens, String separator) {
        if (tokens == null)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tokens.length; i++) {
            if (i > 0)
                sb.append(separator);
            sb.append(tokens[i]);
        }
        return sb.toString();
    }

    /**
     * Join the specified tokens into a delimited string.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @return The delimited string.  If <code>tokens</code> is <jk>null</jk>, returns <jk>null</jk>.
     */
    public static String join(int[] tokens, String d) {
        if (tokens == null)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tokens.length; i++) {
            if (i > 0)
                sb.append(d);
            sb.append(tokens[i]);
        }
        return sb.toString();
    }

    /**
     * Join the specified tokens into a delimited string.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @return The delimited string.  If <code>tokens</code> is <jk>null</jk>, returns <jk>null</jk>.
     */
    public static String join(Collection<?> tokens, String d) {
        if (tokens == null)
            return null;
        return join(tokens, d, new StringBuilder()).toString();
    }

    /**
     * Joins the specified tokens into a delimited string and writes the output to the specified string builder.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @param sb The string builder to append the response to.
     * @return The same string builder passed in as <code>sb</code>.
     */
    public static StringBuilder join(Collection<?> tokens, String d, StringBuilder sb) {
        if (tokens == null)
            return sb;
        for (Iterator<?> iter = tokens.iterator(); iter.hasNext();) {
            sb.append(iter.next());
            if (iter.hasNext())
                sb.append(d);
        }
        return sb;
    }

    /**
     * Joins the specified tokens into a delimited string.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @return The delimited string.  If <code>tokens</code> is <jk>null</jk>, returns <jk>null</jk>.
     */
    public static String join(Object[] tokens, char d) {
        if (tokens == null)
            return null;
        return join(tokens, d, new StringBuilder()).toString();
    }

    /**
     * Join the specified tokens into a delimited string and writes the output to the specified string builder.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @param sb The string builder to append the response to.
     * @return The same string builder passed in as <code>sb</code>.
     */
    public static StringBuilder join(Object[] tokens, char d, StringBuilder sb) {
        if (tokens == null)
            return sb;
        for (int i = 0; i < tokens.length; i++) {
            if (i > 0)
                sb.append(d);
            sb.append(tokens[i]);
        }
        return sb;
    }

    /**
     * Join the specified tokens into a delimited string.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @return The delimited string.  If <code>tokens</code> is <jk>null</jk>, returns <jk>null</jk>.
     */
    public static String join(int[] tokens, char d) {
        if (tokens == null)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tokens.length; i++) {
            if (i > 0)
                sb.append(d);
            sb.append(tokens[i]);
        }
        return sb.toString();
    }

    /**
     * Join the specified tokens into a delimited string.
     *
     * @param tokens The tokens to join.
     * @param d The delimiter.
     * @return The delimited string.  If <code>tokens</code> is <jk>null</jk>, returns <jk>null</jk>.
     */
    public static String join(Collection<?> tokens, char d) {
        if (tokens == null)
            return null;
        StringBuilder sb = new StringBuilder();
        for (Iterator<?> iter = tokens.iterator(); iter.hasNext();) {
            sb.append(iter.next());
            if (iter.hasNext())
                sb.append(d);
        }
        return sb.toString();
    }

    /**
     * Calls {@link #toString()} on the specified object if it's not null.
     *
     * @param o The object to convert to a string.
     * @return The object converted to a string, or <jk>null</jk> if the object was null.
     */
    public static String toString(Object o) {
        return (o == null ? null : o.toString());
    }
}

Related

  1. join(Collection strings, String separator)
  2. join(Collection strings, String separator)
  3. join(Collection strings, String separator)
  4. join(Collection strs)
  5. join(Collection toJoin, String joinBy)
  6. join(Collection values)
  7. join(Collection collection, String separator)
  8. join(Collection data)
  9. join(Collection c, String separator)