Java Iterable Join join(final Iterable tokens, final String delimiter)

Here you can find the source of join(final Iterable tokens, final String delimiter)

Description

join

License

Apache License

Declaration

public static String join(final Iterable<? extends Object> tokens, final String delimiter) 

Method Source Code

//package com.java2s;
/*/*  w ww  . ja va  2  s .com*/
 * Copyright 2009-2016 DigitalGlobe, Inc.
 *
 * 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.*;

public class Main {
    /**
     * Joins an array of doubles into a string with the given delimiter.
     * 
     * There has got to be a better way. :(
     * 
     * @param tokens
     * @param delimiter
     * @return
     */
    public static String join(final double[] tokens, final String delimiter) {
        StringBuffer buffer = new StringBuffer(String.valueOf(tokens[0]));
        for (int i = 1; i < tokens.length; i++) {
            buffer.append(delimiter).append(String.valueOf(tokens[i]));
        }
        return buffer.toString();
    }

    public static String join(final int[] tokens, final String delimiter) {
        StringBuffer buffer = new StringBuffer(String.valueOf(tokens[0]));
        for (int i = 1; i < tokens.length; i++) {
            buffer.append(delimiter).append(String.valueOf(tokens[i]));
        }
        return buffer.toString();
    }

    public static <T> String join(final T[] tokens, final String delimiter) {
        StringBuffer buffer = new StringBuffer(tokens[0].toString());
        for (int i = 1; i < tokens.length; i++) {
            buffer.append(delimiter).append(String.valueOf(tokens[i]));
        }
        return buffer.toString();
    }

    public static String join(final Iterable<? extends Object> tokens, final String delimiter) {
        Iterator<? extends Object> oIter;
        if (tokens == null || (!(oIter = tokens.iterator()).hasNext()))
            return "";
        StringBuilder oBuilder = new StringBuilder(String.valueOf(oIter.next()));
        while (oIter.hasNext()) {
            oBuilder.append(delimiter).append(oIter.next());
        }
        return oBuilder.toString();
    }

    public static String toString(final Map<?, ?> tokens) {
        StringBuilder buf = new StringBuilder("{ ");
        String comma = "";
        for (Map.Entry<?, ?> e : tokens.entrySet()) {
            buf.append(comma + e.getKey() + ":" + e.getValue());
            comma = ", ";
        }
        return buf.toString();
    }
}

Related

  1. join(CharSequence separator, Iterable strings)
  2. join(final CharSequence sep, final Iterable src)
  3. join(final Iterable elements, final String separator)
  4. join(final Iterable iterable, final CharSequence separator)
  5. join(final Iterable objects, final CharSequence separator)
  6. join(final Iterable iterable, final String separator)
  7. join(final Iterable... iterables)
  8. join(final Iterable items, final String separator)
  9. join(final Iterable iterable, final String separator)