Java Collection Intersect getIntersection( final Collection> collections)

Here you can find the source of getIntersection( final Collection> collections)

Description

get Intersection

License

Open Source License

Parameter

Parameter Description
collections a parameter

Return

Collection

Declaration

public static Collection<?> getIntersection(
        final Collection<? extends Collection<? extends Object>> collections) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester//w ww .  j  av  a2  s.  c  o m
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * 
     * @param collections
     * @return Collection
     */
    public static Collection<?> getIntersection(
            final Collection<? extends Collection<? extends Object>> collections) {
        final List<Collection<?>> collectionsList = new ArrayList<Collection<?>>(collections);
        final Collection<Object> intersection = new HashSet<>();

        if (collectionsList.size() != 0) {
            intersection.addAll(collectionsList.get(0));
        }

        for (int i = 1; i < collections.size() && intersection.size() > 0; i++) {
            intersection.retainAll(new HashSet<>(collectionsList.get(i)));
        }

        return intersection;
    }
}

Related

  1. getIntersection(Collection collection1, Collection collection2)
  2. hasIntersection(Collection s1, Collection s2)
  3. hasIntersection(Collection a, Collection b)
  4. hasIntersection(Collection c1, Collection c2)