Class CollectionUtils


  • public class CollectionUtils
    extends Object
    Utility methods for collections conversions.
    • Method Detail

      • toList

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

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

        @NotNull
        public static <T> @NotNull Set<T> toSet​(@NotNull
                                                @NotNull Iterable<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
      • toLinkedSet

        @NotNull
        public static <T> @NotNull Set<T> toLinkedSet​(@NotNull
                                                      @NotNull Iterable<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
      • 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
      • 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:
        newHashMap(int), newLinkedHashSet(int)
      • 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:
        newHashMap(int), newHashSet(int)
      • newHashMap

        @NotNull
        public static <K,​V> @NotNull Map<K,​V> newHashMap​(int capacity)
        Creates a new, empty HashMap with expected capacity.

        The returned map uses the default load factor of 0.75, and its capacity is large enough to add expected number of elements without resizing.

        Parameters:
        capacity - the expected number of elements
        Throws:
        IllegalArgumentException - if capacity is negative
        See Also:
        newHashSet(int), newLinkedHashSet(int)
      • toIterable

        @NotNull
        public static <T> @NotNull Iterable<T> toIterable​(@NotNull
                                                          @NotNull Iterator<T> iterator)
        Convert an Iterator to an Iterable.

        This method is not thread-safe

        Parameters:
        iterator - iterator to convert
        Returns:
        a single-use iterable for the iterator (representing the remaining elements in the iterator)
        Throws:
        IllegalStateException - when Iterable.iterator() is called more than once
      • toStream

        @NotNull
        public static <T> @NotNull Stream<T> toStream​(@NotNull
                                                      @NotNull Iterable<T> iterable)
        Generates a (non-parallel) Stream for the Iterable
        Parameters:
        iterable - iterable to convert
        Returns:
        the stream
      • toStream

        @NotNull
        public static <T> @NotNull Stream<T> toStream​(@NotNull
                                                      @NotNull Iterator<T> iterator)
        Generates a (non-parallel) Stream for the Iterable

        This method is not thread-safe

        Parameters:
        iterator - iterator to convert
        Returns:
        the stream (representing the remaining elements in the iterator)