Class CollectionUtil


  • public class CollectionUtil
    extends Object
    Author:
    Yohann Chastagnier
    • Method Detail

      • reverse

        public static <T> List<T> reverse​(List<T> list)
        Reverse the given list and returns it.
        Type Parameters:
        T - the type of the instance contained into the specified list
        Parameters:
        list - the list to reverse
        Returns:
        the specified list that has been reversed (same instance)
      • isEmpty

        public static <T> boolean isEmpty​(Collection<T> collection)
        Checks if the given collection is not instanced or empty
        Type Parameters:
        T - the type of the instance contained into the specified collection
        Parameters:
        collection - the collection to verify
        Returns:
        true if specified collection is empty, false otherwise
      • isNotEmpty

        public static <T> boolean isNotEmpty​(Collection<T> collection)
        Checks if the given collection is instanced and not empty
        Type Parameters:
        T - the type of the instance contained into the specified collection
        Parameters:
        collection - the collection to verify
        Returns:
        true if specified collection is not empty, false otherwise
      • splitList

        public static <T> List<List<T>> splitList​(List<T> collection)
        Splits a collection into several collections. (Particularly useful for limitations of database around the "in" clause)
        Type Parameters:
        T - the concrete type of the items in the list.
        Parameters:
        collection - the collection to split
        Returns:
        the slices of the specified collection
      • split

        public static <T> Collection<Collection<T>> split​(Collection<T> collection)
        Splits a collection into several collections. (Particularly useful for limitations of database around the "in" clause)
        Type Parameters:
        T - the concrete type of the items in the collection.
        Parameters:
        collection - the collection to split
        Returns:
        the slices of the specified collection
      • split

        public static <T> Collection<Collection<T>> split​(Collection<T> collection,
                                                          int collectionSizeMax)
        Splits a collection into several collections. (Particularly useful for limitations of database around the "in" clause)
        Type Parameters:
        T - the concrete type of the items in the collection.
        Parameters:
        collection - the collection to split
        collectionSizeMax - the maximum elements in slice
        Returns:
        the slices of the specified collection
      • addAllIgnoreNull

        @SafeVarargs
        public static <T> boolean addAllIgnoreNull​(Collection<? super T> c,
                                                   T... elements)
        Null elements are not taking into account.
        Type Parameters:
        T - the concrete type of the items.
        Parameters:
        c - collection in which the items will be added
        elements - the items to add
        Returns:
        true of the items are added, false otherwise.
        See Also:
        Collections.addAll(java.util.Collection, Object[])
      • union

        public static <T> List<T> union​(List<T> list1,
                                        List<T> list2)
        Makes an union between both of the given lists.
        The result contains unique values.
        Type Parameters:
        T - the type of the items in the list.
        Parameters:
        list1 - the first list.
        list2 - the second list.
        Returns:
        the union between the two lists.
      • union

        public static <T> Collection<T> union​(Collection<T> col1,
                                              Collection<T> col2)
        Makes an union between both of the given collections.
        The result contains unique values.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        col1 - the first collection.
        col2 - the second collection.
        Returns:
        the union between the two collections.
      • intersection

        public static <T> List<T> intersection​(List<T> list1,
                                               List<T> list2)
        Makes an intersection between both of the given lists.
        The result contains unique values.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        list1 - the first list.
        list2 - the second list.
        Returns:
        the intersection between the two lists.
      • intersection

        public static <T> Collection<T> intersection​(Collection<T> col1,
                                                     Collection<T> col2)
        Makes an intersection between both of the given collections.
        The result contains unique values.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        col1 - the first collection.
        col2 - the second collection.
        Returns:
        the intersection between the two collections.
      • intersection

        public static <T> List<T> intersection​(List<T> list1,
                                               List<T> list2,
                                               Function<T,​Object> discriminator)
        Makes an intersection between both of the given lists.
        The result contains unique values.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        list1 - the first list.
        list2 - the second list.
        discriminator - get the discriminator data.
        Returns:
        the intersection between the two lists.
      • intersection

        public static <T> Collection<T> intersection​(Collection<T> col1,
                                                     Collection<T> col2,
                                                     Function<T,​Object> discriminator)
        Makes an intersection between both of the given collections.
        The result contains unique values.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        col1 - the first collection.
        col2 - the second collection.
        discriminator - get the discriminator data.
        Returns:
        the intersection between the two collections.
      • indexOf

        public static <T> int indexOf​(List<T> list,
                                      Predicate<T> predicate)
        Finds the first index in the given List which matches the given predicate.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        list - the List to search, may not be null.
        predicate - the predicate to use, may not be null.
        Returns:
        the first index of an Object in the List which matches the predicate.
      • indexOf

        public static <T> int indexOf​(List<T> list,
                                      Predicate<T> predicate,
                                      int firstIndex)
        Finds the first index in the given List which matches the given predicate.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        list - the List to search, may not be null.
        predicate - the predicate to use, may not be null.
        firstIndex - an integer representing the first index from which the search starts.
        Returns:
        the first index of an Object in the List which matches the predicate.
      • findFirst

        public static <T> Optional<T> findFirst​(List<T> list,
                                                Predicate<T> predicate)
        Gets the optional first element in the given List which matches the given predicate.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        list - the List to search, may not be null.
        predicate - the predicate to use, may not be null.
        Returns:
        the optional Object in the List which matches the predicate.
      • findFirst

        public static <T> Optional<T> findFirst​(List<T> list,
                                                Predicate<T> predicate,
                                                int firstIndex)
        Gets the optional first element in the given List which matches the given predicate and the position is at or after the first given index.
        Type Parameters:
        T - the type of the items in the list
        Parameters:
        list - the List to search, may not be null.
        predicate - the predicate to use, may not be null.
        firstIndex - an integer representing the first index from which the search starts.
        Returns:
        the optional Object in the List which matches the predicate.