Interface ICollectionTools


  • public interface ICollectionTools
    ICollectionTools offers default implementations of Collection functionality.
    See Also:
    Collection
    • Method Summary

      Static Methods 
      Modifier and Type Method Description
      static <E> boolean containsIf​(java.util.Collection<E> coll, java.util.function.Predicate<? super E> predicate)
      Determines whether the list contains a matching element.
      static <E> int countIf​(java.util.Collection<E> coll, java.util.function.Predicate<? super E> predicate)
      Counts how many elements in the list match the predicate.
      static <E,​C extends java.util.Collection<E>>
      C
      filter​(java.util.Collection<E> coll, java.util.function.Predicate<? super E> predicate, java.util.function.Supplier<C> factory)
      Create a new list by applying the specified filter to all elements.
      static <E,​R,​C extends java.util.Collection<R>>
      IList<R>
      filterMap​(java.util.Collection<E> coll, java.util.function.Predicate<E> filter, java.util.function.Function<E,​R> func)
      Create a new list by applying the specified filter first and then the mapping function to all elements selected.
      static <E,​R,​C extends java.util.Collection<R>>
      C
      filterMap​(java.util.Collection<E> coll, java.util.function.Predicate<E> filter, java.util.function.Function<E,​R> func, java.util.function.Supplier<C> factory)
      Create a new collection by applying the specified filter first and then the mapping function to all elements selected.
      static <E,​R,​RC extends java.util.Collection<R>,​C extends java.util.Collection<R>>
      C
      flatMap​(java.util.Collection<E> coll, java.util.function.Function<E,​RC> func, java.util.function.Supplier<C> factory)
      Create a new list by applying the specified mapping function to all elements.
      static <E> E getFirst​(java.util.Collection<E> coll)
      Returns the first element stored in the collection.
      static <E> E getFirstOrNull​(java.util.Collection<E> coll)
      Returns the first element stored in the collection.
      static <E> E getIf​(java.util.Collection<E> coll, java.util.function.Predicate<? super E> predicate)
      Returns the first element stored in the collection which matches the predicate.
      static <E> E getSingle​(java.util.Collection<E> coll)
      Returns the only element stored in the collection.
      static <E> E getSingleOrNull​(java.util.Collection<E> coll)
      Returns the only element stored in the collection or null if the collection is empty.
      static <E,​R,​C extends java.util.Collection<R>>
      C
      map​(java.util.Collection<E> coll, java.util.function.Function<E,​R> func, java.util.function.Supplier<C> factory)
      Create a new list by applying the specified mapping function to all elements.
      static <E,​R>
      IList<R>
      mapFilter​(java.util.Collection<E> coll, java.util.function.Function<E,​R> func, java.util.function.Predicate<R> filter)
      Create a new list by applying the specified mapping function to all elements and then filtering it.
      static <E,​R,​C extends java.util.Collection<R>>
      C
      mapFilter​(java.util.Collection<E> coll, java.util.function.Function<E,​R> func, java.util.function.Predicate<R> filter, java.util.function.Supplier<C> factory)
      Create a new collection by applying the specified mapping function to all elements and then filtering it.
    • Method Detail

      • getFirst

        static <E> E getFirst​(java.util.Collection<E> coll)
        Returns the first element stored in the collection. If the collection is empty, a NoSuchElementException is thrown.
        Returns:
        first element stored in the collection
      • getFirstOrNull

        static <E> E getFirstOrNull​(java.util.Collection<E> coll)
        Returns the first element stored in the collection. If the collection is empty, null is returned.
        Returns:
        first element stored in the collection or null if empty
      • getSingle

        static <E> E getSingle​(java.util.Collection<E> coll)
        Returns the only element stored in the collection. If the collection size is not 1, a NoSuchElementException is thrown.
        Returns:
        only element stored in the collection
      • getSingleOrNull

        static <E> E getSingleOrNull​(java.util.Collection<E> coll)
        Returns the only element stored in the collection or null if the collection is empty. If the collection's size is greater than 1, a NoSuchElementException is thrown.
        Returns:
        only element stored in the collection or null if empty
      • getIf

        static <E> E getIf​(java.util.Collection<E> coll,
                           java.util.function.Predicate<? super E> predicate)
        Returns the first element stored in the collection which matches the predicate. If the collection is empty of no element matches, null is returned.
        Returns:
        first element matching the predicate or null if not founds
      • containsIf

        static <E> boolean containsIf​(java.util.Collection<E> coll,
                                      java.util.function.Predicate<? super E> predicate)
        Determines whether the list contains a matching element.
        Parameters:
        predicate - predicate used to search element
        Returns:
        true if the list contains a matching element, false otherwise
      • countIf

        static <E> int countIf​(java.util.Collection<E> coll,
                               java.util.function.Predicate<? super E> predicate)
        Counts how many elements in the list match the predicate.
        Parameters:
        predicate - a predicate which returns true for elements to be counted
        Returns:
        count how many elements in the list match the predicate
      • filter

        static <E,​C extends java.util.Collection<E>> C filter​(java.util.Collection<E> coll,
                                                                    java.util.function.Predicate<? super E> predicate,
                                                                    java.util.function.Supplier<C> factory)
        Create a new list by applying the specified filter to all elements.
        Parameters:
        predicate - filter predicate
        Returns:
        created list
      • map

        static <E,​R,​C extends java.util.Collection<R>> C map​(java.util.Collection<E> coll,
                                                                         java.util.function.Function<E,​R> func,
                                                                         java.util.function.Supplier<C> factory)
        Create a new list by applying the specified mapping function to all elements.
        Parameters:
        func - mapping function
        Returns:
        created list
      • flatMap

        static <E,​R,​RC extends java.util.Collection<R>,​C extends java.util.Collection<R>> C flatMap​(java.util.Collection<E> coll,
                                                                                                                      java.util.function.Function<E,​RC> func,
                                                                                                                      java.util.function.Supplier<C> factory)
        Create a new list by applying the specified mapping function to all elements.
        Parameters:
        func - mapping function
        Returns:
        created list
      • mapFilter

        static <E,​R> IList<R> mapFilter​(java.util.Collection<E> coll,
                                              java.util.function.Function<E,​R> func,
                                              java.util.function.Predicate<R> filter)
        Create a new list by applying the specified mapping function to all elements and then filtering it.
        Parameters:
        func - mapping function
        filter - filter predicate
        Returns:
        created list
      • mapFilter

        static <E,​R,​C extends java.util.Collection<R>> C mapFilter​(java.util.Collection<E> coll,
                                                                               java.util.function.Function<E,​R> func,
                                                                               java.util.function.Predicate<R> filter,
                                                                               java.util.function.Supplier<C> factory)
        Create a new collection by applying the specified mapping function to all elements and then filtering it.
        Parameters:
        func - mapping function
        filter - filter predicate
        factory - factory to create collection
        Returns:
        created list
      • filterMap

        static <E,​R,​C extends java.util.Collection<R>> IList<R> filterMap​(java.util.Collection<E> coll,
                                                                                      java.util.function.Predicate<E> filter,
                                                                                      java.util.function.Function<E,​R> func)
        Create a new list by applying the specified filter first and then the mapping function to all elements selected.
        Parameters:
        filter - filter predicate
        func - mapping function
        Returns:
        created list
      • filterMap

        static <E,​R,​C extends java.util.Collection<R>> C filterMap​(java.util.Collection<E> coll,
                                                                               java.util.function.Predicate<E> filter,
                                                                               java.util.function.Function<E,​R> func,
                                                                               java.util.function.Supplier<C> factory)
        Create a new collection by applying the specified filter first and then the mapping function to all elements selected.
        Parameters:
        filter - filter predicate
        func - mapping function
        factory - factory to create collection
        Returns:
        created list