Class Lists


  • public final class Lists
    extends Object

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

    Author:
    David B. Bracewell
    • Method Detail

      • arrayListOf

        @SafeVarargs
        public static <T> ArrayList<T> arrayListOf​(@NonNull
                                                   @NonNull T... elements)
        Creates an array list 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 array list containing the given elements
      • asArrayList

        public static <T> ArrayList<T> asArrayList​(@NonNull
                                                   @NonNull Stream<? extends T> stream)
        Creates an array list 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 array list containing the given elements
      • asArrayList

        public static <T> ArrayList<T> asArrayList​(@NonNull
                                                   @NonNull Iterator<? extends T> iterator)
        Creates an array list 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 array list containing the given elements
      • asArrayList

        public static <T> ArrayList<T> asArrayList​(@NonNull
                                                   @NonNull Iterable<? extends T> iterable)
        Creates an array list 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 array list containing the given elements
      • asConcurrentList

        public static <T> List<T> asConcurrentList​(@NonNull
                                                   @NonNull Stream<? extends T> stream)
        Creates a copy on write array list 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 copy on write array list containing the given elements
      • asConcurrentList

        public static <T> List<T> asConcurrentList​(@NonNull
                                                   @NonNull Iterator<? extends T> iterator)
        Creates a copy on write array list 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 copy on write array list containing the given elements
      • asConcurrentList

        public static <T> List<T> asConcurrentList​(@NonNull
                                                   @NonNull Iterable<? extends T> iterable)
        Creates a copy on write array list 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 copy on write array list containing the given elements
      • asLinkedList

        public static <T> List<T> asLinkedList​(Stream<? extends T> stream)
        Creates a linked list 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 list containing the given elements
      • asLinkedList

        public static <T> LinkedList<T> asLinkedList​(@NonNull
                                                     @NonNull Iterator<? extends T> iterator)
        Creates a linked list 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 list containing the given elements
      • asLinkedList

        public static <T> LinkedList<T> asLinkedList​(@NonNull
                                                     @NonNull Iterable<? extends T> iterable)
        Creates a linked list 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 list containing the given elements
      • combinations

        public static <T> Iterable<Tuple> combinations​(@NonNull
                                                       @NonNull List<T> list,
                                                       int size)
        Generates all combinations of the given size from the items in the list. For example, given the list [1, 2, 3] and size 2 we would generate the following combinations: [ [1,2], [1,3], [2,3] ]
        Type Parameters:
        T - the type parameter
        Parameters:
        list - the list of items
        size - the number of items in the combination.
        Returns:
        the iterable
      • concurrentListOf

        @SafeVarargs
        public static <T> List<T> concurrentListOf​(@NonNull
                                                   @NonNull T... elements)
        Creates a copy on write array list 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 copy on write array list containing the given elements
      • difference

        public static <E> List<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 list.

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

        public static <T> List<T> ensureSize​(@NonNull
                                             @NonNull List<T> list,
                                             int desiredSize,
                                             T defaultValue)

        Ensures that the size of the given list is at least the supplied desired size. If the list size is smaller, it will add the given default value to the end of the list until list.size() >= desiredSize

        Type Parameters:
        T - the component type of the list
        Parameters:
        list - the list whose size is being checked
        desiredSize - the desired size of the list
        defaultValue - the default value to add to the list to reach the desired size
        Returns:
        the list passed in with size greater than or equal to the desired size
      • linkedListOf

        @SafeVarargs
        public static <T> LinkedList<T> linkedListOf​(@NonNull
                                                     @NonNull T... elements)
        Creates a linked list 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 list containing the given elements
      • partition

        public static <T> List<List<T>> partition​(@NonNull
                                                  @NonNull List<T> list,
                                                  int partitionSize)
        Partitions a list into multiple lists of partition size (last list may have a size less than partition size). This method uses subList which means that each partition is a view into the underlying list.
        Type Parameters:
        T - the list component type
        Parameters:
        list - the list to partition
        partitionSize - the partition size
        Returns:
        A list of partitioned lists
      • sampleWithReplacement

        public static <E> List<E> sampleWithReplacement​(@NonNull
                                                        @NonNull List<? extends E> list,
                                                        int N)
        Samples the given list for N items allowing replacement
        Type Parameters:
        E - the list type parameter
        Parameters:
        list - the list of elements to sample
        N - the number of elements to sample
        Returns:
        the list of sampled elements
      • transform

        public static <I,​O> List<O> transform​(@NonNull
                                                    @NonNull List<? extends I> list,
                                                    @NonNull
                                                    @NonNull SerializableFunction<? super I,​? extends O> converter)
        Transforms the given list with the given function
        Type Parameters:
        I - the input list type parameter
        O - the transformed list type parameter
        Parameters:
        list - the input list
        converter - the function to convert the input elements to the output elements
        Returns:
        the transformed list (lazy transformation)
      • union

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

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

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