Java Set Difference diffAbyB(Set setA, Set setB)

Here you can find the source of diffAbyB(Set setA, Set setB)

Description

A = {1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7, 8}.

License

Apache License

Parameter

Parameter Description
T the generic type
setA the set a
setB the set b

Return

the sets the

Declaration


public static <T> Set<T> diffAbyB(Set<T> setA, Set<T> setB) 

Method Source Code

//package com.java2s;
/*/*from   w  w w. java2  s  . c  om*/
Copyright 2014 Array-Utilities
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    
 * */

import java.util.Collection;
import java.util.Collections;

import java.util.Iterator;
import java.util.LinkedHashSet;

import java.util.Set;

public class Main {
    /**
     *  A = {1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7, 8}. 
     * To find the difference A - B of these two sets, we begin by writing all of the elements of A, 
     * and then take away every element of A that is also an element of B. Since A shares the elements 
     * 3, 4 and 5 with B, this gives us the set difference 
     * A - B = {1, 2 }.
     *
     * @param <T> the generic type
     * @param setA the set a
     * @param setB the set b
     * @return the sets the
     */

    public static <T> Set<T> diffAbyB(Set<T> setA, Set<T> setB) {

        // SetA empty & SetB has values
        if (isEmpty(setA) && !isEmpty(setB)) {
            return Collections.unmodifiableSet(setB);
        }

        // SetA has values & SetB is empty
        if (!isEmpty(setA) && isEmpty(setB)) {
            return Collections.unmodifiableSet(setA);
        }

        // Both set are empty
        if (isEmpty(setA) && isEmpty(setB)) {
            return Collections.unmodifiableSet(new LinkedHashSet<T>());
        }

        Set<T> setC = new LinkedHashSet<T>();
        Iterator<T> iterA = setA.iterator();
        Iterator<T> iterB = setB.iterator();

        while (iterA.hasNext()) {
            setC.add(iterA.next());
        }
        while (iterB.hasNext()) {
            T o = iterB.next();
            if (setC.contains(o)) {
                setC.remove(o);
            }
        }
        return Collections.unmodifiableSet(setC);
    }

    /**
     * Checks if is empty.
     *
     * @param <E>
     *            the element type
     * @param collection
     *            the collection
     * @return true, if is empty
     */
    public static <E> boolean isEmpty(Collection<? super E> collection) {

        if ((collection.size() == 0) || (collection == null))
            return true;
        return false;
    }
}

Related

  1. computeDifference(Set a, Set b, String prefix, boolean exactMatch)
  2. difference(final Set set1, final Set set2)
  3. difference(final Set first, final Set second)
  4. difference(Set a, Set b)
  5. difference(Set first, Set second)