Java Set Difference difference(final Set first, final Set second)

Here you can find the source of difference(final Set first, final Set second)

Description

Returns a set containing all the elements which are not contained by the two given sets.

License

Open Source License

Parameter

Parameter Description
first the first set
second the second set
T the type of element

Return

the difference between two sets

Declaration

public static <T> Set<T> difference(final Set<T> first, final Set<T> second) 

Method Source Code


//package com.java2s;
/*//w ww  .  j  a v a 2  s  . co m
 * "Copyright (c) 2013   Capgemini Technology Services (hereinafter "Capgemini")
 *
 * License/Terms of Use
 * Permission is hereby granted, free of charge and for the term of intellectual
 * property rights on the Software, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to use, copy, modify and
 * propagate free of charge, anywhere in the world, all or part of the Software
 * subject to the following mandatory conditions:
 *
 * -   The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * Any failure to comply with the above shall automatically terminate the license
 * and be construed as a breach of these Terms of Use causing significant harm to
 * Capgemini.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, PEACEFUL ENJOYMENT,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Capgemini shall not be used in
 * advertising or otherwise to promote the use or other dealings in this Software
 * without prior written authorization from Capgemini.
 *
 * These Terms of Use are subject to French law.
 *
 * IMPORTANT NOTICE: The WUIC software implements software components governed by
 * open source software licenses (BSD and Apache) of which CAPGEMINI is not the
 * author or the editor. The rights granted on the said software components are
 * governed by the specific terms and conditions specified by Apache 2.0 and BSD
 * licenses."
 */

import java.util.*;

public class Main {
    /**
     * <p>
     * Returns a set containing all the elements which are not contained by the two given sets.
     * </p>
     *
     * @param first the first set
     * @param second the second set
     * @param <T> the type of element
     * @return the difference between two sets
     */
    public static <T> Set<T> difference(final Set<T> first, final Set<T> second) {
        final Set<T> retval = new HashSet<T>();

        for (final T e : first) {
            if (!second.contains(e)) {
                retval.add(e);
            }
        }

        for (final T e : second) {
            if (!first.contains(e)) {
                retval.add(e);
            }
        }

        return retval;
    }
}

Related

  1. computeDifference(Set a, Set b, String prefix, boolean exactMatch)
  2. diffAbyB(Set setA, Set setB)
  3. difference(final Set set1, final Set set2)
  4. difference(Set a, Set b)
  5. difference(Set first, Set second)
  6. difference(Set s1, Set s2)
  7. difference(Set setA, Set setB)