Set operations: union, intersection, difference

 
import java.util.LinkedHashSet;
import java.util.Set;

public class SetOps {

  /**
   * @return the union of s1 and s2. (The union of two sets is the set
   *         containing all of the elements contained in either set.)
   */
  public static <T> Set<T> union(Set<T> s1, Set<T> s2) {
    Set<T> union = new LinkedHashSet<T>(s1);
    union.addAll(s2);
    return union;
  }

  /**
   * @return the intersection of s1 and s2. (The intersection of two sets is
   *         the set containing only the elements common to both sets.)
   */
  public static <T> Set<T> intersection(Set<T> s1, Set<T> s2) {
    Set<T> intersection = new LinkedHashSet<T>(s1);
    intersection.retainAll(s2);
    return intersection;
  }

  /**
   * @return the (asymmetric) set difference of s1 and s2. (For example, the
   *         set difference of s1 minus s2 is the set containing all of the
   *         elements found in s1 but not in s2.)
   */
  public static <T> Set<T> difference(Set<T> s1, Set<T> s2) {
    Set<T> difference = new LinkedHashSet<T>(s1);
    difference.removeAll(s2);
    return difference;
  }
}
  
Home 
  Java Book 
    Runnable examples  

Collection Set:
  1. Convert set to an Array
  2. Convert Set into List
  3. Convert a set to a synchronized Set
  4. Is a particular value in(exist) Set
  5. Iterator a set
  6. Maximum element of Set
  7. Minimum element of a Set
  8. Remove all elements from a set
  9. Remove element from Set
  10. Remove one set from another set
  11. Remove all the elements from one set to another set
  12. Union(Add) all the elements from one set to another set
  13. Intersection of one set and another set
  14. Set operations: union, intersection, difference
  15. Set operations: symmetric difference, is subset, is superset
  16. Size of a Set