Java Collection Intersect intersection(final Collection c1, final Collection c2)

Here you can find the source of intersection(final Collection c1, final Collection c2)

Description

return a such that a exists in c1 and a exists in c2.

License

Open Source License

Declaration

public static <T> List<T> intersection(final Collection<T> c1, final Collection<T> c2) 

Method Source Code

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

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

public class Main {
    /**//from w w  w. j a  va  2 s  .c o  m
     * return a such that a exists in c1 and a exists in c2.
     * always returns a new collection.
     */
    public static <T> List<T> intersection(final Collection<T> c1, final Collection<T> c2) {
        if (c1 == null || c2 == null) {
            return new ArrayList<>();
        }
        if (c1.size() == 0 || c2.size() == 0) {
            return new ArrayList<>();
        }
        final List<T> intersection = new ArrayList<>();
        for (final T current : c1) {
            if (c2.contains(current)) {
                intersection.add(current);
            }
        }
        return intersection;
    }
}

Related

  1. intersection(Collection a, Collection b)
  2. intersection(Collection values1, Collection values2)
  3. intersection(final Collection a, final Collection b)
  4. intersection(final Collection c1, final Collection c2)
  5. intersection(final Collection a, final Collection b)
  6. intersectionSize(Collection c1, Collection c2)
  7. intersects(Collection c1, Collection c2)
  8. intersects(Collection c0, Collection c1)
  9. intersectsWith(Collection S1, Collection S2)