Description

Returns the intersection set of a series of input collections.

License

Open Source License

Parameter

Parameter Description
A Key type
collections Series of input collections

Return

Intersection set of input collections

Declaration

public static <A> Set<A> intersect(Collection<A>... collections) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*from   ww  w  .j  av a 2 s .  c o  m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Returns the intersection set of a series of input collections. There is no order guarantee.
     *
     * @param <A> Key type
     * @param collections Series of input collections
     * @return Intersection set of input collections
     */
    public static <A> Set<A> intersect(Collection<A>... collections) {
        if (collections.length == 0)
            return new LinkedHashSet<A>();

        Set<A> intersectionSet = new LinkedHashSet<A>();
        intersectionSet.addAll(collections[0]);

        for (int i = 1; i < collections.length; i++) {
            intersectionSet.retainAll(collections[i]);
        }

        return intersectionSet;
    }
}

Related

  1. hasIntersection(Collection col1, Collection col2)
  2. hasIntersection(final Collection a, final Collection b)
  3. intersect(Collection c1, Collection c2)
  4. intersect(Collection c1, Collection c2)
  5. intersect(Collection collection, Collection otherCollection)
  6. intersect(Collection a, Collection b)
  7. intersect(Collection collA, Collection collB, Collection target)
  8. intersect(Collection collection1, Collection collection2)
  9. intersect(Collection t1, Collection t2)