Java Collection Join joinCollections(final Collection target, final Collection... collections)

Here you can find the source of joinCollections(final Collection target, final Collection... collections)

Description

Join several all provided collections in the first one provided.
The instance of the target collection is returned to ease use in foreach loops.

License

Open Source License

Parameter

Parameter Description
T the type of the elements contained in the collection.
target the target collection
collections the collections of items to add in the target collection.

Return

the first collection, with the other items of the collection added.

Declaration

@SafeVarargs
public static <T> Collection<T> joinCollections(final Collection<T> target,
        final Collection<? extends T>... collections) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/*  w w w .ja  va  2  s  . co  m*/
     * Join several all provided collections in the first one provided.<br />
     * The instance of the target collection is returned to ease use in foreach loops.
     * @param <T>
     *        the type of the elements contained in the collection.
     * @param target
     *        the target collection
     * @param collections
     *        the collections of items to add in the target collection.
     * @return the first collection, with the other items of the collection added.
     */
    @SafeVarargs
    public static <T> Collection<T> joinCollections(final Collection<T> target,
            final Collection<? extends T>... collections) {
        if (target == null) {
            throw new IllegalArgumentException("Cannot join collection in null target");
        }
        if (collections.length == 0) {
            return target;
        }

        for (final Collection<? extends T> collection : collections) {
            target.addAll(collection);
        }
        return target;
    }
}

Related

  1. joinAsString(Collection collection, String separator)
  2. joinAsStrings(Collection values, String separator)
  3. joinCol(Collection lst, String delim)
  4. joinCollection(Collection collection, char delimiter)
  5. joinCollection(Collection a)
  6. joinCollectionToString(Collection list, String str)
  7. joinCommaDelimitedList(Collection list)
  8. joinEmptyItemIncluded(Collection stringCollection, String delimiter)
  9. joinList(@SuppressWarnings("rawtypes") Collection c)