Java Collection Intersect intersection(Collection> collectionOfCollections)

Here you can find the source of intersection(Collection> collectionOfCollections)

Description

Returns the intersection of the Collection s of the given container Collection

License

Apache License

Parameter

Parameter Description
collectionOfCollections a parameter

Declaration

public static <E> Set<E> intersection(Collection<? extends Collection<E>> collectionOfCollections) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz//  ww  w.  jav 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.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;

import java.util.Set;

public class Main {
    /**
     * Returns the intersection of the {@link Collection}s of the given container {@link Collection}
     * 
     * @param collectionOfCollections
     * @return
     */
    public static <E> Set<E> intersection(Collection<? extends Collection<E>> collectionOfCollections) {
        //
        Set<E> retset = new LinkedHashSet<E>();

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

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

        //
        return retset;
    }

    /**
     * Returns the intersection of two given {@link Set} instances by iterating over the smaller given {@link Set} and testing on
     * the {@link Set#contains(Object)} method of the larger {@link Set}.
     * 
     * @param set1
     * @param set2
     * @return new {@link Set} instance
     */
    public static <E> Set<E> intersection(Set<E> set1, Set<E> set2) {
        //
        final Set<E> retset = new LinkedHashSet<E>();

        //
        if (set1 != null && set2 != null) {
            //
            Iterable<E> iterable;
            Collection<E> collection;
            if (set1.size() > set2.size()) {
                collection = set1;
                iterable = set2;
            } else {
                collection = set2;
                iterable = set1;
            }

            //
            for (E element : iterable) {
                if (collection.contains(element)) {
                    retset.add(element);
                }
            }
        }

        //
        return retset;
    }

    /**
     * Returns the intersection of the {@link Collection}s of the given container {@link Collection}
     * 
     * @param collections
     * @return
     */
    public static <E> Set<E> intersection(Collection<E>... collections) {
        //
        final Set<E> retset = new LinkedHashSet<E>();

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

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

        return retset;
    }

    /**
     * Returns the intersection of the {@link Collection}s of the given container {@link Collection}
     * 
     * @param collectionFirst
     * @param collectionSecond
     * @return new {@link Set} instance
     */
    @SuppressWarnings("unchecked")
    public static <E> Set<E> intersection(Collection<E> collectionFirst, Collection<E> collectionSecond) {
        return intersection(Arrays.asList(collectionFirst, collectionSecond));
    }

    /**
     * Adds the elements from the given {@link Iterable} to the given {@link Set}. If the given {@link Set} is null, a new
     * {@link LinkedHashSet} is returned. If the given {@link Iterable} is null, nothing is added.
     * 
     * @param set
     *          {@link Set}
     * @param elementIterable
     *          {@link Iterable}
     * @return given {@link Set} instance or new {@link LinkedHashSet}
     */
    @SuppressWarnings("unchecked")
    public static <E> Set<E> addAll(Set<? extends E> set, Iterable<? extends E> elementIterable) {
        Set<E> retset = set != null ? (Set<E>) set : new LinkedHashSet<E>();
        if (elementIterable != null) {
            for (E element : elementIterable) {
                retset.add(element);
            }
        }
        return retset;
    }

    /**
     * Does call {@link Set#retainAll(Collection)} on the given {@link Set} instance. If null is given as {@link Set} null is
     * returned as result. If null is given as retainable element {@link Collection} the given {@link Set} is cleared.
     * 
     * @param set
     * @param retainableCollection
     * @return given instance or null if null is given
     */
    public static <E> Set<E> retainAll(Set<E> set, Collection<? extends E> retainableCollection) {
        final Set<E> retset = set;
        if (retset != null) {
            if (retainableCollection != null) {
                retset.retainAll(retainableCollection);
            } else {
                retset.clear();
            }
        }
        return retset;
    }

    /**
     * Returns the given {@link Set}, or a new instance if the given {@link Set} is null. The returned {@link Set} will have the
     * given elements added.
     * 
     * @param set
     * @param elements
     * @return given {@link Set} or new instance if null
     */
    public static <E> Set<E> add(Set<E> set, E... elements) {
        Set<E> retset = set != null ? set : new LinkedHashSet<E>();
        if (elements != null) {
            for (E element : elements) {
                retset.add(element);
            }
        }
        return retset;
    }
}

Related

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