1 /** 2 * Copyright 2010-2013 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.kuali.common.util; 17 18 import java.util.HashSet; 19 import java.util.Set; 20 21 public class SetUtils { 22 23 /** 24 * Return a new <code>Set</code> containing only those elements that appear in both <code>a</code> and <code>b</code> 25 */ 26 public static <T> Set<T> intersection(Set<T> a, Set<T> b) { 27 Set<T> result = new HashSet<T>(); 28 result.addAll(a); 29 result.retainAll(b); 30 return result; 31 } 32 33 /** 34 * Return a new <code>Set</code> containing all of the elements from both <code>a</code> and <code>b</code> 35 */ 36 public static <T> Set<T> union(Set<T> a, Set<T> b) { 37 Set<T> result = new HashSet<T>(); 38 result.addAll(a); 39 result.addAll(b); 40 return result; 41 } 42 43 /** 44 * Return a new <code>Set</code> containing only those elements that appear in <code>a</code> but not <code>b</code> 45 */ 46 public static <T> Set<T> difference(Set<T> a, Set<T> b) { 47 Set<T> result = new HashSet<T>(); 48 result.addAll(a); 49 result.removeAll(b); 50 return result; 51 } 52 53 }