Interface ICollectionTools


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

    • getFirst

      static <E> E getFirst(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(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(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(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(Collection<E> coll, 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(Collection<E> coll, 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(Collection<E> coll, 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 Collection<E>> C filter(Collection<E> coll, Predicate<? super E> predicate, 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 Collection<R>> C map(Collection<E> coll, Function<E,R> func, 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 Collection<R>, C extends Collection<R>> C flatMap(Collection<E> coll, Function<E,RC> func, 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(Collection<E> coll, Function<E,R> func, 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 Collection<R>> C mapFilter(Collection<E> coll, Function<E,R> func, Predicate<R> filter, 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 Collection<R>> IList<R> filterMap(Collection<E> coll, Predicate<E> filter, 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 Collection<R>> C filterMap(Collection<E> coll, Predicate<E> filter, Function<E,R> func, 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