Java Iterable Join join(Iterable parts, String delimiter)

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

Description

Join a number of (string representations of) items to a single string using a delimiter

License

Apache License

Parameter

Parameter Description
T the type of items to join
parts the parts to join
delimiter the delimiter to use

Return

the joined string

Declaration

public static <T> String join(Iterable<T> parts, String delimiter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010, 2012 Institute for Dutch Lexicology
 *
 * 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./*w w  w .  j a va 2  s .c  o  m*/
 *******************************************************************************/

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**
     * Join a number of (string representations of) items to a single string using a delimiter
     *
     * @param <T>
     *            the type of items to join
     * @param parts
     *            the parts to join
     * @param delimiter
     *            the delimiter to use
     * @return the joined string
     */
    public static <T> String join(Iterable<T> parts, String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator<T> iter = parts.iterator();
        while (iter.hasNext()) {
            builder.append(iter.next().toString());
            if (!iter.hasNext()) {
                break;
            }
            builder.append(delimiter);
        }
        return builder.toString();
    }

    /**
     * Join a number of (string representations of) items to a single string using a delimiter
     *
     * @param <T>
     *            the type of items to join
     * @param parts
     *            the array of parts to join
     * @param delimiter
     *            the delimiter to use
     * @return the joined string
     */
    public static <T> String join(T[] parts, String delimiter) {
        StringBuilder builder = new StringBuilder();
        for (T t : parts) {
            if (builder.length() > 0)
                builder.append(delimiter);
            builder.append(t.toString());
        }
        return builder.toString();
    }

    /**
     * Join keys and values from a map to produce a string.
     *
     * @param map the map to join
     * @param delimiter how to delimit map entries
     * @param keyValueDelimiter what to put between key and value
     * @return the resulting string
     */
    public static <T, U> String join(Map<T, U> map, String delimiter, String keyValueDelimiter) {
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<T, U> e : map.entrySet()) {
            if (builder.length() > 0)
                builder.append(delimiter);
            builder.append(e.getKey().toString()).append(keyValueDelimiter).append(e.getValue().toString());
        }
        return builder.toString();
    }

    /**
     * Join keys and values from a map to produce a string.
     *
     * Uses an equals sign between key and value.
     *
     * @param map the map to join
     * @param delimiter how to delimit map entries
     * @return the resulting string
     */
    public static <T, U> String join(Map<T, U> map, String delimiter) {
        return join(map, delimiter, "=");
    }

    /**
     * Join keys and values from a map to produce a string.
     *
     * Uses an equals sign between key and value and a semicolon and
     * space between entries.
     *
     * @param map the map to join
     * @return the resulting string
     */
    public static <T, U> String join(Map<T, U> map) {
        return join(map, "; ");
    }
}

Related

  1. join(Iterable coll, String sep)
  2. join(Iterable coll, String sep)
  3. join(Iterable coll, String separator)
  4. join(Iterable items, String delimiter)
  5. join(Iterable iterable)
  6. join(Iterable sequence, String delimiter)
  7. join(Iterable values, String separator)
  8. join(Iterable values, String separator)
  9. join(Iterator iterator, String separator)