Class CollectionUtil

java.lang.Object
dev.architectury.loom.util.collection.CollectionUtil

public final class CollectionUtil extends Object
Stream-like utilities for working with collections.
  • Constructor Details

    • CollectionUtil

      public CollectionUtil()
  • Method Details

    • find

      public static <E> Optional<E> find(Iterable<? extends E> collection, Predicate<? super E> filter)
      Finds the first element matching the predicate.
      Type Parameters:
      E - the element type
      Parameters:
      collection - the collection to be searched
      filter - the predicate to be matched
      Returns:
      the first matching element, or empty if none match
    • indexOf

      public static <E> int indexOf(List<? extends E> list, Predicate<? super E> filter)
      Finds the index of the first element matching the predicate.
      Type Parameters:
      E - the element type
      Parameters:
      list - the list to be searched
      filter - the predicate to be matched
      Returns:
      the index of the first matching element, or -1 if none match
    • map

      public static <A, B> List<B> map(Iterable<? extends A> collection, Function<? super A,? extends B> transform)
      Transforms the collection with a function.
      Type Parameters:
      A - the source type
      B - the target type
      Parameters:
      collection - the source collection
      transform - the transformation function
      Returns:
      a mutable list with the transformed entries
    • mapTo

      public static <A, B, C extends Collection<B>> C mapTo(Iterable<? extends A> collection, C target, Function<? super A,? extends B> transform)
      Transforms the collection with a function, storing the results in a target collection.
      Type Parameters:
      A - the source type
      B - the target type
      C - the target collection type
      Parameters:
      collection - the source collection
      target - the target collection
      transform - the transformation function
      Returns:
      the target collection
    • filter

      public static <A> List<A> filter(Collection<? extends A> collection, Predicate<? super A> filter)
      Filters a list based on a predicate.
      Type Parameters:
      A - the element type
      Parameters:
      collection - the source collection
      filter - the filtering predicate
      Returns:
      a mutable list with the filtered entries
    • single

      public static <A> Optional<A> single(Iterable<? extends A> iterable)
      Gets the single element of an iterable.
      Type Parameters:
      A - the element type
      Parameters:
      iterable - the iterable
      Returns:
      the single element, or empty if there are no elements or multiple elements
    • first

      public static <A> Optional<A> first(Iterable<? extends A> iterable)
      Gets the first element of an iterable.
      Type Parameters:
      A - the element type
      Parameters:
      iterable - the iterable
      Returns:
      the first element, or empty if there are no elements