Class Sets


  • public class Sets
    extends Object
    Utilities for sets.
    Author:
    Roger Levy, Bill MacCartney
    • Method Detail

      • map

        public static <E,​F> Set<F> map​(Set<E> oldSet,
                                             Function<E,​F> lambda)
      • cross

        public static <E,​F> Set<Pair<E,​F>> cross​(Set<E> s1,
                                                             Set<F> s2)
        Returns the set cross product of s1 and s2, as Pairs
      • diff

        public static <E> Set<E> diff​(Set<E> s1,
                                      Set<E> s2)
        Returns the difference of sets s1 and s2.
      • symmetricDiff

        public static <E> Set<E> symmetricDiff​(Set<E> s1,
                                               Set<E> s2)
        Returns the symmetric difference of sets s1 and s2 (i.e. all elements that are in only one of the two sets)
      • union

        public static <E> Set<E> union​(Set<E> s1,
                                       Set<E> s2)
        Returns the union of sets s1 and s2.
      • intersection

        public static <E> Set<E> intersection​(Set<E> s1,
                                              Set<E> s2)
        Returns the intersection of sets s1 and s2.
      • intersects

        public static <E> boolean intersects​(Set<E> s1,
                                             Set<E> s2)
        Returns true if there is at least element that is in both s1 and s2. Faster than calling intersection(Set,Set) if you don't need the contents of the intersection.
      • powerSet

        public static <E> Set<Set<E>> powerSet​(Set<E> s)
        Returns the powerset (the set of all subsets) of set s.
      • assertEquals

        public static <E> void assertEquals​(Set<E> first,
                                            Set<E> second,
                                            String firstName,
                                            String secondName,
                                            boolean outputShared,
                                            Supplier<String> errorMessage)
        Tests whether two sets are equal. If not, throws an assertion and gives a detailed report on the differences. May be long depending on the sizes of the sets!
        Parameters:
        first - a set to compare
        second - a set to compare against
        firstName - the name of the first set, used if an error occurs
        secondName - the name of the second set, used if an error occurs
        outputShared - output the common values for the two sets
        errorMessage - a Supplier of an error message, in case it is expensive to generate
      • main

        public static void main​(String[] args)