Returns the difference of two sets (S1\S2). - Java java.util

Java examples for java.util:Set Operation

Description

Returns the difference of two sets (S1\S2).

Demo Code


//package com.java2s;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**/*  ww  w  .  j a va2 s . com*/
     * Returns the difference of two sets (S1\S2). It calls {@link Set#removeAll(java.util.Collection)} but returns a
     * new set containing the elements of the difference.
     * 
     * @param set1
     *            the first set
     * @param set2
     *            the second set
     * @return the difference of the sets
     */
    public static <V> Set<V> difference(Set<V> set1, Set<V> set2) {
        Set<V> difference = new HashSet<V>();
        difference.addAll(set1);
        difference.removeAll(set2);
        return difference;
    }
}

Related Tutorials