Java Collection Join join(final String separator, final Collection objects)

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

Description

Returns a string of the form elt1.toString() [sep elt2.toString() ...

License

Open Source License

Parameter

Parameter Description
separator the string to use to separate objects
objects a collection of objects. the element order is defined by the iterator over objects
T the type of the objects

Return

a non-null string

Declaration

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

Method Source Code

//package com.java2s;
/*/*from  w  w  w  . j  av a2  s .c  om*/
 * Copyright (c) 2017 NCIC, Institute of Computing Technology, Chinese Academy of Sciences
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.AbstractList;

import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * join an array of strings given a seperator
     * @param separator the string to insert between each array element
     * @param strings the array of strings
     * @return a string, which is the joining of all array values with the separator
     */
    public static String join(String separator, String[] strings) {
        return join(separator, strings, 0, strings.length);
    }

    public static String join(String separator, String[] strings, int start, int end) {
        if ((end - start) == 0) {
            return "";
        }
        StringBuilder ret = new StringBuilder(strings[start]);
        for (int i = start + 1; i < end; ++i) {
            ret.append(separator);
            ret.append(strings[i]);
        }
        return ret.toString();
    }

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

    /**
     * Returns a string of the values in joined by separator, such as A,B,C
     *
     * @param separator separator character
     * @param doubles   the array with values
     * @return a string with the values separated by the separator
     */
    public static String join(String separator, double[] doubles) {
        if (doubles == null || doubles.length == 0)
            return "";
        else {
            StringBuilder ret = new StringBuilder();
            ret.append(doubles[0]);
            for (int i = 1; i < doubles.length; ++i) {
                ret.append(separator);
                ret.append(doubles[i]);
            }
            return ret.toString();
        }
    }

    /**
     * Returns a string of the form elt1.toString() [sep elt2.toString() ... sep elt.toString()] for a collection of
     * elti objects (note there's no actual space between sep and the elti elements).  Returns
     * "" if collection is empty.  If collection contains just elt, then returns elt.toString()
     *
     * @param separator the string to use to separate objects
     * @param objects a collection of objects.  the element order is defined by the iterator over objects
     * @param <T> the type of the objects
     * @return a non-null string
     */
    public static <T> String join(final String separator, final Collection<T> objects) {
        if (objects.isEmpty()) { // fast path for empty collection
            return "";
        } else {
            final Iterator<T> iter = objects.iterator();
            final T first = iter.next();

            if (!iter.hasNext()) // fast path for singleton collections
                return first.toString();
            else { // full path for 2+ collection that actually need a join
                final StringBuilder ret = new StringBuilder(first.toString());
                while (iter.hasNext()) {
                    ret.append(separator);
                    ret.append(iter.next().toString());
                }
                return ret.toString();
            }
        }
    }

    public static <T> String join(final String separator, final T... objects) {
        return join(separator, Arrays.asList(objects));
    }

    /**
     * Create a new list that contains the elements of left along with elements elts
     * @param left a non-null list of elements
     * @param elts a varargs vector for elts to append in order to left
     * @return A newly allocated linked list containing left followed by elts
     */
    public static <T> List<T> append(final List<T> left, T... elts) {
        final List<T> l = new LinkedList<T>(left);
        l.addAll(Arrays.asList(elts));
        return l;
    }

    /**
     * Returns a {@link List List&lt;Integer&gt;} representation of an primitive int array.
     * @param values the primitive int array to represent.
     * @return never code {@code null}. The returned list will be unmodifiable yet it will reflect changes in values in the original array yet
     *   you cannot change the values
     */
    public static List<Integer> asList(final int... values) {
        if (values == null)
            throw new IllegalArgumentException("the input array cannot be null");
        return new AbstractList<Integer>() {

            @Override
            public Integer get(final int index) {
                return values[index];
            }

            @Override
            public int size() {
                return values.length;
            }
        };
    }

    /**
     * Returns a {@link List List&lt;Double&gt;} representation of an primitive double array.
     * @param values the primitive int array to represent.
     * @return never code {@code null}. The returned list will be unmodifiable yet it will reflect changes in values in the original array yet
     *   you cannot change the values.
     */
    public static List<Double> asList(final double... values) {
        if (values == null)
            throw new IllegalArgumentException("the input array cannot be null");
        return new AbstractList<Double>() {

            @Override
            public Double get(final int index) {
                return values[index];
            }

            @Override
            public int size() {
                return values.length;
            }
        };
    }

    /**
     * Adds element from an array into a collection.
     *
     * In the event of exception being throw due to some element, <code>dest</code> might have been modified by
     * the successful addition of element before that one.
     *
     * @param dest the destination collection which cannot be <code>null</code> and should be able to accept
     *             the input elements.
     * @param elements the element to add to <code>dest</code>
     * @param <T>  collection type element.
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by <code>dest</code>.
     * @throws ClassCastException if the class of any of the elements
     *         prevents it from being added to <code>dest</code>.
     * @throws NullPointerException if any of the elements is <code>null</code> and <code>dest</code>
     *         does not permit <code>null</code> elements
     * @throws IllegalArgumentException if some property of any of the elements
     *         prevents it from being added to this collection
     * @throws IllegalStateException if any of the elements cannot be added at this
     *         time due to insertion restrictions.
     * @return <code>true</code> if the collection was modified as a result.
     */
    public static <T> boolean addAll(Collection<T> dest, T... elements) {
        boolean result = false;
        for (final T e : elements) {
            result = dest.add(e) | result;
        }
        return result;
    }
}

Related

  1. join(final String d, final Collection c)
  2. join(final String delimiter, final Collection elements)
  3. join(final String s, final Collection c)
  4. join(final String sep, final Collection c)
  5. join(final String sep, final Collection strs)
  6. join(final String separator, final Collection objects)
  7. join(final String separator, final Collection objects)
  8. join(Iterable collection, String delimiter)
  9. join(Iterable collection)