Java Collection Intersect intersection(Collection... collections)

Here you can find the source of intersection(Collection... collections)

Description

Returns a new List with only this elements which are in all of the given Collection s

License

Apache License

Parameter

Parameter Description
collections a parameter

Return

new instance

Declaration

public static <E> List<E> intersection(Collection<E>... collections) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz//w ww .  j av  a  2  s .  c  o m
 * 
 * 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.ArrayList;

import java.util.Collection;

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

public class Main {
    /**
     * Returns a new {@link List} with only this elements which are in all of the given {@link Collection}s
     * 
     * @param collections
     * @return new {@link List} instance
     */
    public static <E> List<E> intersection(Collection<E>... collections) {
        //
        final List<E> retlist = new ArrayList<E>();

        //
        if (collections.length > 0) {
            //
            Collection<E> collection = collections[0];
            if (collection != null) {
                retlist.addAll(collection);
            }

            //
            for (int ii = 1; ii < collections.length && !retlist.isEmpty(); ii++) {
                //
                Collection<E> collectionOther = collections[ii];
                if (collectionOther != null) {
                    retlist.retainAll(collectionOther);
                }
            }
        }

        //
        return retlist;
    }

    /**
     * Returns a new {@link List} with only this elements which are in all of the given {@link Collection}s
     * 
     * @param collectionOfCollections
     * @return new {@link List} instance
     */
    public static <E> List<E> intersection(Collection<? extends Collection<E>> collectionOfCollections) {
        //
        List<E> retlist = new ArrayList<E>();

        //
        if (!collectionOfCollections.isEmpty()) {
            //
            final Iterator<? extends Collection<E>> listCollectionIterator = collectionOfCollections.iterator();
            Collection<E> collection = listCollectionIterator.next();
            if (collection != null) {
                retlist.addAll(collection);
            }

            //
            while (listCollectionIterator.hasNext()) {
                Collection<E> collectionOther = listCollectionIterator.next();
                if (collectionOther != null) {
                    retlist.retainAll(collectionOther);
                }
            }
        }

        //
        return retlist;
    }

    /**
     * Adds all the elements from the {@link Iterable} to the given {@link List} instance. If the given {@link List} instance is
     * null a new {@link ArrayList} is created. <br>
     * If null is given as {@link Iterable} nothing will be added to the {@link List}.
     * 
     * @param list
     * @param iterable
     * @return given {@link List} instance or a new {@link ArrayList} if null is given
     */
    @SuppressWarnings("unchecked")
    public static <E> List<E> addAll(List<? extends E> list, Iterable<? extends E> iterable) {
        final List<E> retlist = (List<E>) (list != null ? list : new ArrayList<E>());
        if (iterable != null) {
            for (E element : iterable) {
                retlist.add(element);
            }
        }
        return retlist;
    }

    /**
     * Similar to {@link #add(List, Object...)}
     * 
     * @param list
     * @param elements
     * @return
     */
    public static <E> List<E> addAll(List<? extends E> list, E... elements) {
        return add(list, elements);
    }

    /**
     * Similar to {@link #add(List, int, Object...)}
     * 
     * @param list
     * @param index
     * @param elements
     * @return
     */
    public static <E> List<E> addAll(List<? extends E> list, int index, E... elements) {
        return add(list, index, elements);
    }

    /**
     * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will
     * contain all elements of the given {@link List} and additionally the further given element. <br>
     * <br>
     * This function will return always an instance, even if the given list is null.
     * 
     * @param list
     * @param element
     * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null
     */
    public static <E> List<E> add(List<? extends E> list, E element) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        retlist.add(element);

        //
        return retlist;
    }

    /**
     * Similar to {@link #add(List, Object)} allowing to specify an index position
     * 
     * @param list
     * @param index
     * @param element
     * @return the given {@link List} instance or a new one
     */
    public static <E> List<E> add(List<? extends E> list, int index, E element) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        while (list.size() < index) {
            list.add(null);
        }

        //
        retlist.add(index, element);

        //
        return retlist;
    }

    /**
     * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will
     * contain all elements of the given {@link List} and additionally all further given elements. <br>
     * <br>
     * This function will return always a instance, even if the given list is null.
     * 
     * @param list
     * @param elements
     * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null
     */
    public static <E> List<E> add(List<? extends E> list, E... elements) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        if (elements != null) {
            for (E element : elements) {
                retlist.add(element);
            }
        }

        //
        return retlist;
    }

    /**
     * Similar to {@link #add(List, Object...)} allowing to specify an index position
     * 
     * @param list
     * @param index
     * @param elements
     * @return the given {@link List} instance or a new one
     */
    public static <E> List<E> add(List<? extends E> list, int index, E... elements) {
        //
        @SuppressWarnings("unchecked")
        List<E> retlist = (List<E>) list;

        //
        if (list == null) {
            retlist = new ArrayList<E>();
        }

        //
        while (list.size() < index) {
            list.add(null);
        }

        //
        int relative = 0;
        if (elements != null) {
            for (E element : elements) {
                retlist.add(index + relative, element);
                relative++;
            }
        }

        //
        return retlist;
    }
}

Related

  1. intersectInto(C into, Collection... from)
  2. intersection( Collection> coll)
  3. intersection(Collection a, Collection b)
  4. intersection(Collection> collectionOfCollections)
  5. intersection(Collection> availableValuesByDescriptor)
  6. intersection(Collection> sets)
  7. intersection(Collection a, Collection b)
  8. intersection(Collection a, Collection b)
  9. intersection(Collection a, Collection b)