Class Collect


  • public final class Collect
    extends Object

    Convenience methods for manipulating Collections.

    Author:
    David B. Bracewell
    • Method Detail

      • addAll

        public static <T> Collection<T> addAll​(@NonNull
                                               @NonNull Collection<T> collection,
                                               @NonNull
                                               @NonNull Iterable<? extends T> iterable)
        Adds all elements in the iterable to the given collection
        Type Parameters:
        T - the element type parameter
        Parameters:
        collection - the collection to add values to
        iterable - the iterable of elements to add
        Returns:
        the collection with new elements added
      • addAll

        public static <T> Collection<T> addAll​(@NonNull
                                               @NonNull Collection<T> collection,
                                               @NonNull
                                               @NonNull Iterator<? extends T> iterator)
        Adds all elements in the iterator to the given collection
        Type Parameters:
        T - the element type parameter
        Parameters:
        collection - the collection to add values to
        iterator - the iterator of elements to add
        Returns:
        the collection with new elements added
      • addAll

        public static <T> Collection<T> addAll​(@NonNull
                                               @NonNull Collection<T> collection,
                                               @NonNull
                                               @NonNull Stream<? extends T> stream)
        Adds all elements in the stream to the given collection
        Type Parameters:
        T - the element type parameter
        Parameters:
        collection - the collection to add values to
        stream - the stream of elements to add
        Returns:
        the collection with new elements added
      • asCollection

        public static <T> Collection<T> asCollection​(@NonNull
                                                     @NonNull Iterable<? extends T> iterable)
        Wraps an iterable as a collection.
        Type Parameters:
        T - the element type parameter
        Parameters:
        iterable - the iterable to wrap
        Returns:
        the wrapped iterable
      • newCollection

        public static <T extends Collection> T newCollection​(Class<T> collectionClass)

        Creates a default instance of a collection type. If the passed in class is an implementation then that implementation is created using the no-arg constructor. Interfaces (e.g. Set and List) have default implementations assigned and returned.

        Type Parameters:
        T - the type parameter
        Parameters:
        collectionClass - the collection class
        Returns:
        t t