Java Set Intersect intersect(Collection set1, Collection set2)

Here you can find the source of intersect(Collection set1, Collection set2)

Description

intersect

License

Apache License

Declaration

public static <T> Collection<T> intersect(Collection<T> set1, Collection<T> set2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;
import java.util.Collections;

import java.util.LinkedHashSet;

import java.util.Map;
import java.util.Set;

public class Main {
    public static <T> Collection<T> intersect(Collection<T> set1, Collection<T> set2) {
        if (isEmpty(set1) || isEmpty(set2)) {
            return Collections.emptyList();
        }//from  www .j  a  v a2s  .co  m

        Set<T> intersection = new LinkedHashSet<T>();
        for (T item : set1) {
            if (item != null && set2.contains(item)) {
                intersection.add(item);
            }
        }
        return intersection;
    }

    public static boolean isEmpty(Collection<? extends Object> col) {
        return !isNotEmpty(col);
    }

    public static boolean isEmpty(Map<? extends Object, ? extends Object> map) {
        return map == null || map.isEmpty();
    }

    public static boolean isNotEmpty(Collection<? extends Object> col) {
        return col != null && col.size() > 0;
    }

    public static boolean isNotEmpty(Map<? extends Object, ? extends Object> map) {
        return map != null && map.size() > 0;
    }
}

Related

  1. getIntersection(Set a, Set b)
  2. getIntersection(Set s1, Set s2)
  3. intersect(final Set firstSet, final Set secondSet)
  4. intersect(Set one, Set two)
  5. intersect(Set set1, Set set2)
  6. intersect(Set a, Set b)