Class SetOperations


  • public class SetOperations
    extends java.lang.Object
    Utility class containing set-math operations.

    See probabilitycouse or britannica for definitions and theory.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> java.util.Set<E> difference​(java.util.Set<E> first, java.util.Set<E> second)
      Create a difference set of the items contained only in either of the two sets, but not both.
      static <E> boolean disjoint​(java.util.Set<E> first, java.util.Set<E> second)
      Check that the two sets are disjoint, meaning they have no items in common.
      static <E> java.util.Set<E> intersect​(java.util.Set<E> set, java.util.Set<E>... with)
      Get the intersection between the sets.
      static <A,​B>
      java.util.Set<Pair<A,​B>>
      product​(java.util.Set<A> a, java.util.Set<B> b)
      Calculate the cartesian product of the two sets.
      static <E> boolean subset​(java.util.Set<E> set, java.util.Set<E> of)
      Check if the first set is a subset of the second.
      static <E> java.util.Set<E> subtract​(java.util.Set<E> set, java.util.Set<E>... with)  
      static <E> java.util.Set<E> union​(java.util.Set<E>... of)
      Get the union of all the set's provided.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • union

        @Nonnull
        @SafeVarargs
        public static <E> java.util.Set<E> union​(@Nonnull
                                                 java.util.Set<E>... of)
        Get the union of all the set's provided. Essentially a set of all elements in each set in the input.
        Type Parameters:
        E - The item type.
        Parameters:
        of - Sets to make union of.
        Returns:
        The union set.
      • intersect

        @Nonnull
        @SafeVarargs
        public static <E> java.util.Set<E> intersect​(@Nonnull
                                                     java.util.Set<E> set,
                                                     @Nonnull
                                                     java.util.Set<E>... with)
        Get the intersection between the sets. These are the items contains in all of the sets provided.
        Type Parameters:
        E - The item type.
        Parameters:
        set - The main set to make intersection with.
        with - Sets to intersect with.
        Returns:
        The intersected set.
      • subtract

        @Nonnull
        @SafeVarargs
        public static <E> java.util.Set<E> subtract​(@Nonnull
                                                    java.util.Set<E> set,
                                                    @Nonnull
                                                    java.util.Set<E>... with)
      • difference

        public static <E> java.util.Set<E> difference​(@Nonnull
                                                      java.util.Set<E> first,
                                                      @Nonnull
                                                      java.util.Set<E> second)
        Create a difference set of the items contained only in either of the two sets, but not both.
        Type Parameters:
        E - The item type.
        Parameters:
        first - The first set.
        second - The second set.
        Returns:
        The difference set.
      • product

        @Nonnull
        public static <A,​B> java.util.Set<Pair<A,​B>> product​(@Nonnull
                                                                         java.util.Set<A> a,
                                                                         @Nonnull
                                                                         java.util.Set<B> b)
        Calculate the cartesian product of the two sets.
        Type Parameters:
        A - The element type in first set
        B - The element type in second set
        Parameters:
        a - The first set
        b - The second set
        Returns:
        The cartesian product.
      • subset

        public static <E> boolean subset​(@Nonnull
                                         java.util.Set<E> set,
                                         @Nonnull
                                         java.util.Set<E> of)
        Check if the first set is a subset of the second.
        Type Parameters:
        E - The item type.
        Parameters:
        set - The set to check.
        of - The set to check against.
        Returns:
        True if the first set is a subset of the second.
      • disjoint

        public static <E> boolean disjoint​(@Nonnull
                                           java.util.Set<E> first,
                                           @Nonnull
                                           java.util.Set<E> second)
        Check that the two sets are disjoint, meaning they have no items in common. Or that the intersection between the sets are empty.
        Type Parameters:
        E - The item type.
        Parameters:
        first - The first set.
        second - The second set.
        Returns:
        True if the two sets are disjoint.