Class SetUtil


  • public final class SetUtil
    extends Object
    Utility class for operations on Sets.
    • Method Detail

      • merge

        public static <E> Set<E> merge​(Set<E> a,
                                       Set<E> b)
        Returns a set containing all elements from set a and set b. If there are duplicates in both sets, the elements from the set a are favored.
        Type Parameters:
        E - the Set's element type
        Parameters:
        a - set a
        b - set b
        Returns:
        a Set containing the specified elements
      • merge

        @SafeVarargs
        public static <E> Set<E> merge​(Set<E> a,
                                       E... b)
        Returns a set containing all elements from a and the element b. b is ignored if an equal element is already contained in the set a.
        Type Parameters:
        E - the Set's element type
        Parameters:
        a - set a
        b - set b
        Returns:
        a Set containing the specified elements
      • difference

        public static <E> Set<E> difference​(Set<E> a,
                                            Collection<E> b)
        Returns a set containing all elements from set a that are not in set b.
        Type Parameters:
        E - the Set's element type
        Parameters:
        a - set a
        b - set b
        Returns:
        a Set containing the specified elements
      • difference

        @SafeVarargs
        public static <E> Set<E> difference​(Set<E> a,
                                            E... b)
        Returns a set containing all elements from set a that are not b.
        Type Parameters:
        E - the Set's element type
        Parameters:
        a - set a
        b - set b
        Returns:
        a Set containing the specified elements
      • nthElement

        public static <E> E nthElement​(Set<E> set,
                                       int n)
        Returns the n-th element from set set. Throws a IndexOutOfBoundsException if n is negative or greater than the set's cardinality.
        Type Parameters:
        E - the Set's element type
        Parameters:
        set - a set
        n - specifies the element to be taken
        Returns:
        n-th element from set set
        Throws:
        IndexOutOfBoundsException - if n is negative or greater than the set's cardinality
      • firstElement

        public static <E> E firstElement​(Set<E> set)
        Returns the first element from set set. Returns null if set is empty.
        Type Parameters:
        E - the Set's element type
        Parameters:
        set - a set
        Returns:
        first element from set set
      • cartesianProduct

        public static <A,​B> Set<Pair<A,​B>> cartesianProduct​(Set<A> a,
                                                                        Set<B> b)
        Returns the cartesian product of the two sets a and b. This is the set of all ordered Pairs (x,y) where x is in a and y is in b.
        Type Parameters:
        A - the type of the elements in set a
        B - the type of the elements in set b
        Parameters:
        a - first set for the cartesian product
        b - second set for the cartesian product
        Returns:
        the cartesian product of the two sets a and b