Returns non-overlapping elements of two collections. - Java java.util

Java examples for java.util:Collection Element

Description

Returns non-overlapping elements of two collections.

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        Collection coll1 = java.util.Arrays.asList("asdf", "java2s.com");
        Collection coll2 = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(nonOverLap(coll1, coll2));
    }//from w  ww . j ava2 s .  c  o m

    /**
     * Returns non-overlapping elements of two collections.
     *
     * @param <T> the generic type
     * @param coll1 the coll1
     * @param coll2 the coll2
     * @return the collection
     */
    public static <T> Collection<T> nonOverLap(Collection<T> coll1,
            Collection<T> coll2) {
        Collection<T> result = union(coll1, coll2);
        result.removeAll(intersect(coll1, coll2));

        return result;
    }

    /**
     * Returns union of two collections.
     *
     * @param <T> the generic type
     * @param coll1 the coll1
     * @param coll2 the coll2
     * @return the collection
     */
    public static <T> Collection<T> union(Collection<T> coll1,
            Collection<T> coll2) {
        Set<T> union = new HashSet<T>(coll1);
        union.addAll(new HashSet<T>(coll2));

        return union;
    }

    /**
     * Returns intersection of two collections.
     *
     * @param <T> the generic type
     * @param coll1 the coll1
     * @param coll2 the coll2
     * @return the collection
     */
    public static <T> Collection<T> intersect(Collection<T> coll1,
            Collection<T> coll2) {
        Set<T> intersection = new HashSet<T>(coll1);
        intersection.retainAll(new HashSet<T>(coll2));

        return intersection;
    }
}

Related Tutorials