Class SetUtils


  • public class SetUtils
    extends Object
    Utility methods for Set conversions.
    • Method Detail

      • toSet

        @NotNull
        public static <T> @NotNull Set<T> toSet​(@NotNull
                                                @NotNull Iterable<? extends T> iterable)
        Convert an iterable to a set. The returning set is mutable and supports all optional operations.
        Type Parameters:
        T - the type of the elements
        Parameters:
        iterable - the iterable to convert
        Returns:
        the set
      • toSet

        @NotNull
        public static <T> @NotNull Set<T> toSet​(@NotNull
                                                @NotNull Iterator<T> iterator)
        Convert an iterator to a set. The returning set is mutable and supports all optional operations.
        Type Parameters:
        T - the type of the elements
        Parameters:
        iterator - the iterator to convert
        Returns:
        the set
      • toSet

        @SafeVarargs
        @NotNull
        public static <T> @NotNull Set<T> toSet​(@NotNull
                                                @NotNull T... elements)
        Convert a vararg list of items to a set. The returning set is mutable and supports all optional operations.
        Type Parameters:
        T - the type of the elements
        Parameters:
        elements - elements to convert
        Returns:
        the set
      • newHashSet

        @NotNull
        public static <K> @NotNull Set<K> newHashSet​(int capacity)
        Creates a new, empty HashSet with expected capacity.

        The returned set is large enough to add expected no. of elements without resizing.

        Parameters:
        capacity - the expected number of elements
        Throws:
        IllegalArgumentException - if capacity is negative
        See Also:
        MapUtils.newHashMap(int), newLinkedHashSet(int)
      • newIdentityHashSet

        @NotNull
        public static <E> @NotNull Set<E> newIdentityHashSet()
        Creates a new, empty IdentityHashSet with default size.
      • toLinkedSet

        @NotNull
        public static <T> @NotNull Set<T> toLinkedSet​(@NotNull
                                                      @NotNull Iterable<? extends T> iterable)
        Convert an iterable to a LinkedHashSet. The returning set is mutable and supports all optional operations.
        Type Parameters:
        T - the type of the elements
        Parameters:
        iterable - the iterable to convert
        Returns:
        the linkedHashSet
      • toLinkedSet

        @SafeVarargs
        @NotNull
        public static <T> @NotNull Set<T> toLinkedSet​(@NotNull
                                                      @NotNull T... elements)
        Convert a vararg list of items to a set. The returning set is mutable and supports all optional operations.
        Type Parameters:
        T - the type of the elements
        Parameters:
        elements - elements to convert
        Returns:
        the set
      • newLinkedHashSet

        @NotNull
        public static <K> @NotNull Set<K> newLinkedHashSet​(int capacity)
        Creates a new, empty LinkedHashSet with expected capacity.

        The returned set is large enough to add expected no. of elements without resizing.

        Parameters:
        capacity - the expected number of elements
        Throws:
        IllegalArgumentException - if capacity is negative
        See Also:
        MapUtils.newHashMap(int), newHashSet(int)
      • toTreeSet

        @NotNull
        public static <T extends Comparable> @NotNull TreeSet<T> toTreeSet​(@NotNull
                                                                           @NotNull Iterable<? extends T> iterable)
        Convert an iterable to a TreeSet. The returning set is mutable and supports all optional operations.
        Type Parameters:
        T - the type of the elements
        Parameters:
        iterable - the iterable to convert
        Returns:
        the treeSet
        Throws:
        ClassCastException - if the specified object cannot be compared with the elements currently in this set
        NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
      • union

        @NotNull
        public static <T> @NotNull Set<T> union​(@NotNull
                                                @NotNull Set<T> s1,
                                                @NotNull
                                                @NotNull Set<T> s2)
        Returns a new set containing the union of the two specified sets. The union of two sets is a set containing all the elements of both sets.
        Type Parameters:
        T - the type of elements in the sets
        Parameters:
        s1 - the first set, must not be null
        s2 - the second set, must not be null
        Returns:
        a new set containing the union of the two specified sets
        Throws:
        NullPointerException - if either of the sets is null
      • intersection

        @NotNull
        public static <T> @NotNull Set<T> intersection​(@NotNull
                                                       @NotNull Set<T> s1,
                                                       @NotNull
                                                       @NotNull Set<T> s2)
        Returns a new set containing the intersection of the two specified sets. The intersection of two sets is a set containing only the elements that are present in both sets.
        Type Parameters:
        T - the type of elements in the sets
        Parameters:
        s1 - the first set, must not be null
        s2 - the second set, must not be null
        Returns:
        a new set containing the intersection of the two specified sets
        Throws:
        NullPointerException - if either of the sets is null
      • symmetricDifference

        public static <T> Set<T> symmetricDifference​(Set<T> s1,
                                                     Set<T> s2)
        Returns a new set containing the symmetric difference of the two specified sets. The symmetric difference of two sets is a set containing elements that are in either of the sets, but not in their intersection.
        Type Parameters:
        T - the type of elements in the sets
        Parameters:
        s1 - the first set, must not be null
        s2 - the second set, must not be null
        Returns:
        a new set containing the symmetric difference of the two specified sets
        Throws:
        NullPointerException - if either of the sets is null
      • difference

        public static <T> Set<T> difference​(Set<T> s1,
                                            Set<T> s2)
        Returns a new set containing the difference of the two specified sets. The difference of two sets is a set containing elements that are in the first set but not in the second set.
        Type Parameters:
        T - the type of elements in the sets
        Parameters:
        s1 - the first set, must not be null
        s2 - the second set, must not be null
        Returns:
        a new set containing the difference of the two specified sets
        Throws:
        NullPointerException - if either of the sets is null