Class Sets


  • public final class Sets
    extends Object

    Convenience methods for creating sets and manipulating collections resulting in sets.

    Author:
    David B. Bracewell
    • Method Detail

      • asConcurrentHashSet

        public static <T> Set<T> asConcurrentHashSet​(Iterator<? extends T> iterator)
        Creates a concurrent hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterator - the elements to add to the set
        Returns:
        the new concurrent hash set containing the given elements
      • asConcurrentHashSet

        public static <T> Set<T> asConcurrentHashSet​(Iterable<? extends T> iterable)
        Creates a concurrent hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterable - the elements to add to the set
        Returns:
        the new concurrent hash set containing the given elements
      • asConcurrentHashSet

        public static <T> Set<T> asConcurrentHashSet​(Stream<? extends T> stream)
        Creates a concurrent hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        stream - the elements to add to the set
        Returns:
        the new concurrent hash set containing the given elements
      • asHashSet

        public static <T> Set<T> asHashSet​(Iterator<? extends T> iterator)
        Creates a hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterator - the elements to add to the set
        Returns:
        the new hash set containing the given elements
      • asHashSet

        public static <T> Set<T> asHashSet​(Iterable<? extends T> iterable)
        Creates a hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterable - the elements to add to the set
        Returns:
        the new hash set containing the given elements
      • asHashSet

        public static <T> Set<T> asHashSet​(Stream<? extends T> stream)
        Creates a hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        stream - the elements to add to the set
        Returns:
        the new hash set containing the given elements
      • asLinkedHashSet

        public static <T> LinkedHashSet<T> asLinkedHashSet​(Iterator<? extends T> iterator)
        Creates a linked hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterator - the elements to add to the set
        Returns:
        the new linked hash set containing the given elements
      • asLinkedHashSet

        public static <T> LinkedHashSet<T> asLinkedHashSet​(Iterable<? extends T> iterable)
        Creates a linked hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterable - the elements to add to the set
        Returns:
        the new linked hash set containing the given elements
      • asLinkedHashSet

        public static <T> LinkedHashSet<T> asLinkedHashSet​(Stream<? extends T> stream)
        Creates a linked hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        stream - the elements to add to the set
        Returns:
        the new linked hash set containing the given elements
      • asTreeSet

        public static <T> TreeSet<T> asTreeSet​(Iterator<? extends T> iterator)
        Creates a tree set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterator - the elements to add to the set
        Returns:
        the new tree set containing the given elements
      • asTreeSet

        public static <T> TreeSet<T> asTreeSet​(Iterable<? extends T> iterable)
        Creates a tree hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        iterable - the elements to add to the set
        Returns:
        the new tree hash set containing the given elements
      • asTreeSet

        public static <T> TreeSet<T> asTreeSet​(Stream<? extends T> stream)
        Creates a tree hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        stream - the elements to add to the set
        Returns:
        the new tree hash set containing the given elements
      • concurrentSetOf

        @SafeVarargs
        public static <T> Set<T> concurrentSetOf​(T... elements)
        Creates a concurrent hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        elements - the elements to add to the set
        Returns:
        the new concurrent hash set containing the given elements
      • difference

        public static <E> Set<E> difference​(@NonNull
                                            @NonNull Collection<? extends E> collection1,
                                            @NonNull
                                            @NonNull Collection<? extends E> collection2)

        Retains all items in collection1 that are not in collection2 and returns them as a set.

        Type Parameters:
        E - the component type of the collections
        Parameters:
        collection1 - the first collection of items
        collection2 - the second collection of items
        Returns:
        A set of the collection1 - collection2
      • hashSetOf

        @SafeVarargs
        public static <T> Set<T> hashSetOf​(@NonNull
                                           @NonNull T... elements)
        Creates a hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        elements - the elements to add to the set
        Returns:
        the new hash set containing the given elements
      • intersection

        public static <E> Set<E> intersection​(@NonNull
                                              @NonNull Collection<? extends E> collection1,
                                              @NonNull
                                              @NonNull Collection<? extends E> collection2)

        Retains all items that are in both collection1 and collection2 and returns them as a set.

        Type Parameters:
        E - the component type of the collections
        Parameters:
        collection1 - the first collection of items
        collection2 - the second collection of items
        Returns:
        A set containing the intersection of collection1 and collection2
      • linkedHashSetOf

        @SafeVarargs
        public static <T> LinkedHashSet<T> linkedHashSetOf​(@NonNull
                                                           @NonNull T... elements)
        Creates a linked hash set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        elements - the elements to add to the set
        Returns:
        the new linked hash set containing the given elements
      • newConcurrentHashSet

        public static <T> Set<T> newConcurrentHashSet()
        New concurrent hash set set.
        Type Parameters:
        T - the type parameter
        Returns:
        the set
      • sortedSetOf

        @SafeVarargs
        public static <T> NavigableSet<T> sortedSetOf​(@NonNull
                                                      @NonNull T... elements)
        Creates a tree set of the supplied elements
        Type Parameters:
        T - the component type of the set
        Parameters:
        elements - the elements to add to the set
        Returns:
        the new tree set containing the given elements
      • transform

        public static <E,​R> Set<R> transform​(Set<? extends E> collection,
                                                   SerializableFunction<? super E,​R> transform)

        Transforms a given collection using a supplied transform function returning the results as a set.

        Type Parameters:
        E - the component type of the collection being transformed
        R - the component type of the resulting collection after transformation
        Parameters:
        collection - the collection to be transformed
        transform - the function used to transform elements of type E to R
        Returns:
        A set containing the transformed items of the supplied collection
      • union

        public static <E> Set<E> union​(Collection<? extends E> collection1,
                                       Collection<? extends E> collection2)

        Retains all items in collection1 and collection2 and returns them as a set.

        Type Parameters:
        E - the component type of the collections
        Parameters:
        collection1 - the first collection of items
        collection2 - the second collection of items
        Returns:
        A set of the collection1 + collection2