Here you can find the source of getIntersection(Set
s1
and set s2
.
Parameter | Description |
---|---|
s1 | a set |
s2 | another set |
public static <T> Set<T> getIntersection(Set<T> s1, Set<T> s2)
//package com.java2s; /*/*from w w w .j a v a 2s.c o m*/ * (c) 2017 Michael A. Beck, Sebastian Henningsen * disco | Distributed Computer Systems Lab * University of Kaiserslautern, Germany * All Rights Reserved. * * This software is work in progress and is released in the hope that it will * be useful to the scientific community. It is provided "as is" without * express or implied warranty, including but not limited to the correctness * of the code or its suitability for any particular purpose. * * This software is provided under the MIT License, however, we would * appreciate it if you contacted the respective authors prior to commercial use. * * If you find our software useful, we would appreciate if you mentioned it * in any publication arising from the use of this software or acknowledge * our work otherwise. We would also like to hear of any fixes or useful */ import java.util.List; import java.util.Set; import java.util.HashSet; import java.util.Iterator; public class Main { /** * Returns the intersection of set <code>s1</code> and set <code>s2</code>. * @param s1 a set * @param s2 another set * @return the intersection set */ public static <T> Set<T> getIntersection(Set<T> s1, Set<T> s2) { if (s1 == null || s2 == null) return new HashSet<T>(); Set<T> result = new HashSet<T>(s1); result.retainAll(s2); return result; } /** * Returns the intersection of all sets contained in the list <code>sets</code>. * @param sets a list of sets * @return the intersection of all sets */ public static <T> Set<T> getIntersection(List<Set<T>> sets) { Set<T> result = new HashSet<T>(); Iterator<Set<T>> iter = sets.iterator(); if (iter.hasNext()) { result.addAll(iter.next()); for (; iter.hasNext();) { result.retainAll(iter.next()); } } return result; } }