Here you can find the source of intersection(Set> a, Set> b)
b
as a Hashtable
containing elements contained both a
and b
Parameter | Description |
---|---|
a | <CODE>java.util.Hashtable</CODE> to be examined |
b | <CODE>java.util.Hashtable</CODE> to be examined |
Hashtable
representing intersection of a
and b
public static Set<?> intersection(Set<?> a, Set<?> b)
//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; } }