Java Collection Union union(Collection c1, Collection c2)

Here you can find the source of union(Collection c1, Collection c2)

Description

Adds all elements of two collections to a new LinkedList.

License

Open Source License

Parameter

Parameter Description
c1 the first collection to add elements from.
c2 the second collection to add elements from.
E the type of the collections elements.

Return

a LinkedList containing the elements from both input collections.

Declaration


public static <E> List<E> union(Collection<E> c1, Collection<E> c2) 

Method Source Code

//package com.java2s;
/*//from  ww  w  .  ja  v a 2 s.  com
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-util nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

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

import java.util.Iterator;
import java.util.LinkedHashMap;

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

import java.util.Map;

public class Main {
    /**
     * Adds all elements of two collections to a new LinkedList.
     * 
     * @param c1
     *            the first collection to add elements from.
     * @param c2
     *            the second collection to add elements from.
     * @return a LinkedList containing the elements from both input collections.
     * @param <E>
     *            the type of the collections elements.
     */

    public static <E> List<E> union(Collection<E> c1, Collection<E> c2) {
        List<E> result = new LinkedList<E>();
        result.addAll(c1);
        result.addAll(c2);
        return result;
    }

    /**
     * Same as {@link #union(Iterator)} applied to given collection's iterator.
     * @param maps the collection of maps
     * @param <K> the type of keys
     * @param <V> the type of values
     * @return a new map with all entries from all maps in a collection
     */
    public static <K, V> Map<K, V> union(Collection<Map<K, V>> maps) {
        return union(maps.iterator());
    }

    /**
     * Returns a new map with all entries from all maps in an iterator's range.
     * @param mapsIterator the maps iterator
     * @param <K> the type of keys
     * @param <V> the type of values
     * @return a new map with all entries from all maps in an iterator's range
     */
    public static <K, V> Map<K, V> union(Iterator<Map<K, V>> mapsIterator) {
        return putAllFromAll(new LinkedHashMap<K, V>(), mapsIterator);
    }

    /**
     * Adds all elements of iterator's range to collection.
     * 
     * @param c
     *            the collection to add the iterator's range to.
     * @param i
     *            the iterator whose range is to be added to the given
     *            collection.
     * @return the given collection.
     * @param <T>
     *            the type of the elements given.
     * @param <C> the type of the collection
     */
    public static <T, C extends Collection<T>> C addAll(C c, Iterator<T> i) {
        while (i.hasNext()) {
            c.add(i.next());
        }
        return c;
    }

    /**
     * Construct an iterator of the given type ranging over provided elements.
     * 
     * @param elements
     *            the elements to construct the List from.
     * @return an iterator over the received arguments.
     * @param <T>
     *            the type of elements the iterator will range over.
     */
    public static <T> Iterator<T> iterator(T... elements) {
        return Arrays.asList(elements).iterator();
    }

    /**
     * Puts all entries from all maps in the range of an iterator to a given map.
     * @param map the map
     * @param mapsIterator the maps iterator
     * @param <K> the type of keys
     * @param <V> the type of values
     * @return the given map with all entries from all maps in interator's range
     */
    public static <K, V> Map<K, V> putAllFromAll(Map<K, V> map, Iterator<Map<K, V>> mapsIterator) {
        for (Map<K, V> eachMap : in(mapsIterator)) {
            map.putAll(eachMap);
        }
        return map;
    }

    /**
     * Adapts an {@link Iterator} to an {@link Iterable} for use in enhanced for
     * loops. If {@link Iterable#iterator()} is invoked more than once, an
     * {@link IllegalStateException} is thrown.
     * @param iterator the iterator
     * @param <T> the type of elements
     * @return the iterable for iterator
     */
    public static <T> Iterable<T> in(final Iterator<T> iterator) {
        assert iterator != null;
        class SingleUseIterable implements Iterable<T> {
            private boolean used = false;

            @Override
            public Iterator<T> iterator() {
                if (used) {
                    throw new IllegalStateException("SingleUseIterable already invoked");
                }
                used = true;
                return iterator;
            }
        }
        return new SingleUseIterable();
    }

    /**
     * @param map
     *        a map.
     * @param keysAndValues
     *            a sequence of key and value pairs to be placed into a given Map.
     * 
     * @return the received arguments (interpreted as a sequence of key and
     *         value pairs) in a given map.
     * @param <K>
     *            the type of the Map's key.
     * @param <V>
     *            the type of the Map's value.
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> putAll(Map<K, V> map, Object... keysAndValues) {
        int i = 0;
        while (i != keysAndValues.length) {
            map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]);
            i += 2;
        }
        return map;
    }
}

Related

  1. union(Collection c1, Collection c2)
  2. union(Collection coll)
  3. union(Collection collection1, Collection collection2)
  4. union(Collection> sets)
  5. union(Collection a, Collection b)
  6. union(Collection c1, Collection c2)
  7. union(Collection initial, Collection other)