Here you can find the source of intersect(Set extends T> set1, Set extends T> set2)
Parameter | Description |
---|---|
set1 | input set |
set2 | input set |
public static <T> Set<T> intersect(Set<? extends T> set1, Set<? extends T> set2)
//package com.java2s; /*/* w w w .j ava2s.c o m*/ Copyright 1996-2010 Ariba, Inc. 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. $Id: //ariba/platform/util/core/ariba/util/core/SetUtil.java#16 $ */ import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.Collection; public class Main { /** Perform the operation intersect on two given sets. The operation is nondestructive.<p> If one of the sets is null or empty the method returns an empty set.<p> @param set1 input set @param set2 input set @return the intersect of the given sets. @aribaapi ariba */ public static <T> Set<T> intersect(Set<? extends T> set1, Set<? extends T> set2) { Set<T> s = set(); intersect(set1, set2, s); return s; } /** Perform the operation intersect on two given sets. The operation is nondestructive.<p> If one of the sets is null or empty, nothing would be added to the result set.<p> @param set1 input set @param set2 input set @param result result set @throw IllegalArgumentException when result is <code>null</code>. @aribaapi ariba */ public static <T> void intersect(Set<? extends T> set1, Set<? extends T> set2, Set<T> result) { if (result == null) { throw new IllegalArgumentException("result must not be null"); } if (set1 == null || set2 == null || set1.isEmpty() || set2.isEmpty()) { return; } Set<? extends T> iterateThru = null; Set<? extends T> check = null; if (set1.size() > set2.size()) { iterateThru = set2; check = set1; } else { iterateThru = set1; check = set2; } for (Iterator<? extends T> it = iterateThru.iterator(); it.hasNext();) { T o = it.next(); if (check.contains(o)) { result.add(o); } } } /** Constructs an empty optionally type-safe <code>Set</code>.<p> To construct a type-safe <code>Set</code>:<ul> <li><code>Set<X> typesafe = SetUtil.set()</code> </ul> For a raw <code>Set</code>:<ul> <li><code>Set raw = SetUtil.set()</code> </ul> There will be no compile warnings either way. @return new empty Set @aribaapi ariba */ public static <T> Set<T> set() { return new HashSet<T>(); } /** Constructs a Set capable of holding a specified number of elements. <p/> To construct a type-safe <code>Set</code>:<ul> <li><code>Set<X> typesafe = SetUtil.set(10)</code> </ul> For a raw <code>Set</code>:<ul> <li><code>Set raw = SetUtil.set(10)</code> </ul> There will be no compile warnings either way. @param initialCapacity the number of elements this Set is capable of holding before needing to grow. @return new empty Set with given initial capacity @aribaapi ariba */ public static <T> Set<T> set(int initialCapacity) { return new HashSet<T>(initialCapacity); } /** Creates new Set with the same contents as unique elements in the given Collection. <p/> Given that <code>setOfX</code> is a <code>Set<X></code>, <code>setOfY</code> is a <code>Set<Y></code> (and that <code>Y</code> extends <code>X</code>) and <code>raw</code> is a <code>Set</code>, the following all compile validly. <ul> <li><code>Set<X> typesafe = SetUtil.set(setOfX)</code> <li><code>Set<X> typesafe = SetUtil.<X>set(setOfY)</code> <li><code>Set<?> unknown = SetUtil.set(setOfX)</code> </ul> The following constructs compile with an unchecked warning:<ul> <li><code>Set<X> typesafe = SetUtil.set(raw)</code> <li><code>Set raw = SetUtil.set(setOfX)</code> </ul> @param source source collection @return new Set with same contents as <code>source</code> @exception NullPointerException if the specified Set is null. @aribaapi ariba */ public static <T> Set<T> set(Collection<? extends T> source) { return new HashSet<T>(source); } }