Java Set Intersect intersection(Set a, Set b)

Here you can find the source of intersection(Set a, Set b)

Description

Returns intersection of a and b as a Hashtable containing elements contained both a and b

License

Open Source License

Parameter

Parameter Description
a <CODE>java.util.Hashtable</CODE> to be examined
b <CODE>java.util.Hashtable</CODE> to be examined

Return

Hashtable representing intersection of a and b

Declaration

public static Set<?> intersection(Set<?> a, Set<?> b) 

Method Source Code


//package com.java2s;
/*// w ww  .  ja v a2s .  c om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collections;
import java.util.Set;
import java.util.HashSet;

public class Main {
    /**
     * Returns intersection of <ttCODE>a</CODE> and <CODE>b</CODE> as a
     * <CODE>Hashtable</CODE> containing elements contained both
     * <CODE>a</CODE> and <CODE>b</CODE>
     *
     * @param a <CODE>java.util.Hashtable</CODE> to be examined
     * @param b <CODE>java.util.Hashtable</CODE> to be examined
     *
     * @return <CODE>Hashtable</CODE> representing intersection
     * of <CODE>a</CODE> and <CODE>b</CODE>
     */
    public static Set<?> intersection(Set<?> a, Set<?> b) {
        if (a == null || b == null || a.size() == 0 || b.size() == 0)
            return Collections.EMPTY_SET;

        Set<Object> intersection = new HashSet<Object>();
        Set<?> tester = (a.size() < b.size()) ? a : b;
        Set<?> testant = (a.size() < b.size()) ? b : a;

        for (Object o : tester) {
            if (testant.contains(o))
                intersection.add(o);
        }

        if (intersection.size() == 0)
            return Collections.EMPTY_SET;

        return intersection;
    }
}

Related

  1. intersect(Set set1, Set set2)
  2. intersect(Set a, Set b)
  3. intersect(Set a, Set b)
  4. intersectComparable(Set left, Set right)
  5. intersection(final Set set1, final Set set2)
  6. intersection(Set sa, Set sb)
  7. intersection(Set first, Set second)
  8. intersection(Set s1, Set s2)
  9. intersection(Set setA, Set setB)