Class CollectionUtils


  • public final class CollectionUtils
    extends java.lang.Object
    This class contains a bunch of utility methods for manipulating lists.
    Since:
    3.2-ALPHA-13
    Author:
    Fabrizio Giudici
    Status: stable API
    • Constructor Summary

      Constructors 
      Constructor Description
      CollectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> concat​(java.util.List<? extends T> list, T object)
      Appends a list to an object.
      static <T> java.util.List<T> concatAll​(java.util.Collection<? extends T>... collections)
      Returns a concatenation of the given Collections.
      static <T> T head​(java.util.List<? extends T> list)
      Returns the first element of a list.
      static <T> java.util.Optional<T> optionalHead​(java.util.List<? extends T> list)
      Returns the (optional) first element of a list.
      static <T> java.util.List<T> reversed​(java.util.List<? extends T> list)
      Reverses a list.
      static <T> java.util.List<T> safeSubList​(java.util.List<? extends T> list, int from, int to)
      Return a sublist of the original List, from the given from and to index (not included).
      static <T extends java.lang.Comparable<? super T>>
      java.util.List<T>
      sorted​(java.util.List<? extends T> list)
      Sorts a list.
      static <T> java.util.List<T> sorted​(java.util.List<? extends T> list, java.util.Comparator<? super T> comparator)
      Sorts a list with a given Comparator.
      static <T> java.util.List<java.util.List<T>> split​(java.util.List<? extends T> list, int... boundaries)
      Splits a given List at a set of boundaries.
      static <T> java.util.List<T> tail​(java.util.List<? extends T> list)
      Returns the tail element of a list, that is a list without the first element.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CollectionUtils

        public CollectionUtils()
    • Method Detail

      • concat

        @Nonnull
        public static <T> java.util.List<T> concat​(@Nonnull
                                                   java.util.List<? extends T> list,
                                                   @Nonnull
                                                   T object)
        Appends a list to an object. The resulting list is mutable.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list
        object - the list to append
        Returns:
        the list with the appended object
      • concatAll

        @Nonnull
        public static <T> java.util.List<T> concatAll​(@Nonnull
                                                      java.util.Collection<? extends T>... collections)
        Returns a concatenation of the given Collections.
        Type Parameters:
        T - the static type
        Parameters:
        collections - the input collections
        Returns:
        the concatenation
      • reversed

        @Nonnull
        public static <T> java.util.List<T> reversed​(@Nonnull
                                                     java.util.List<? extends T> list)
        Reverses a list. The resulting list is mutable.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list
        Returns:
        the reversed list
      • sorted

        @Nonnull
        public static <T extends java.lang.Comparable<? super T>> java.util.List<T> sorted​(@Nonnull
                                                                                           java.util.List<? extends T> list)
        Sorts a list. The resulting list is mutable.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list
        Returns:
        the sorted list
        Since:
        3.2-ALPHA-13
      • sorted

        @Nonnull
        public static <T> java.util.List<T> sorted​(@Nonnull
                                                   java.util.List<? extends T> list,
                                                   @Nonnull
                                                   java.util.Comparator<? super T> comparator)
        Sorts a list with a given Comparator. The resulting list is mutable.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list
        comparator - the comparator
        Returns:
        the sorted list
        Since:
        3.2-ALPHA-13
      • optionalHead

        @Nonnull
        public static <T> java.util.Optional<T> optionalHead​(@Nonnull
                                                             java.util.List<? extends T> list)
        Returns the (optional) first element of a list.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list
        Returns:
        the first element
      • head

        @Nonnull
        public static <T> T head​(@Nonnull
                                 java.util.List<? extends T> list)
        Returns the first element of a list.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list (cannot be empty)
        Returns:
        the first element
        Throws:
        java.lang.IllegalArgumentException - if the list is empty
      • tail

        @Nonnull
        public static <T> java.util.List<T> tail​(@Nonnull
                                                 java.util.List<? extends T> list)
        Returns the tail element of a list, that is a list without the first element. The tail of an empty list is an empty list. The resulting list is mutable.
        Type Parameters:
        T - the type of list items
        Parameters:
        list - the list
        Returns:
        the tail of the list
      • safeSubList

        @Nonnull
        public static <T> java.util.List<T> safeSubList​(@Nonnull
                                                        java.util.List<? extends T> list,
                                                        int from,
                                                        int to)
        Return a sublist of the original List, from the given from and to index (not included). If the from index is negative and/or the to index is lower than the from index or if an attempt is made to read before the start or past the end of the list, truncation silently occurs.
        Type Parameters:
        T - the static type
        Parameters:
        list - the original list
        from - the first index (included)
        to - the last index (excluded)
        Returns:
        the sublist
        Since:
        3.2-ALPHA-17
      • split

        @Nonnull
        public static <T> java.util.List<java.util.List<T>> split​(@Nonnull
                                                                  java.util.List<? extends T> list,
                                                                  int... boundaries)
        Splits a given List at a set of boundaries. Each boundary is the starting point of a sublist to be returned.
        Type Parameters:
        T - the static type
        Parameters:
        list - the original list
        boundaries - the boundaries
        Returns:
        a list of sublists
        Since:
        3.2-ALPHA-17