Java List Intersect intersection(List> sets)

Here you can find the source of intersection(List> sets)

Description

intersection

License

Open Source License

Declaration

public static <T> Set<T> intersection(List<Collection<? extends T>> sets) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static <T> Set<T> intersection(List<Collection<? extends T>> sets) {
        if (sets.isEmpty())
            return Collections.emptySet();

        Set<T> accum = new HashSet<>();
        accum.addAll(sets.get(0));/*from   ww w . j  ava2s . c  om*/
        for (int i = 1; i < sets.size(); i++) {
            accum = intersection(accum, sets.get(i));
        }
        return accum;
    }

    public static <T> Set<T> intersection(Collection<? extends T> lhs, Collection<? extends T> rhs) {
        Collection<? extends T> minSet = lhs.size() < rhs.size() ? lhs : rhs;
        Collection<? extends T> maxSet = lhs.size() < rhs.size() ? rhs : lhs;

        HashSet<T> isect = new HashSet<>();
        for (T x : minSet) {
            if (maxSet.contains(x)) {
                isect.add(x);
            }
        }
        return isect;
    }
}

Related

  1. intersectAll(final List collector, final T[] a, final T[] b)
  2. intersectDate(List oDate, List tDate)
  3. intersection(final List list1, final List list2)
  4. intersection(final List list1, final List list2)
  5. intersection(final List list1, final List list2)
  6. intersection(List list1, List list2)
  7. intersection(List list1, List list2)
  8. Intersection(List a, List b)
  9. intersection(List list1, List list2)