Class CollectionUtil
java.lang.Object
dev.architectury.loom.util.collection.CollectionUtil
Stream-like utilities for working with collections.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> List<A> filter(Collection<? extends A> collection, Predicate<? super A> filter) Filters a list based on a predicate.static <E> Optional<E> Finds the first element matching the predicate.static <A> Optional<A> Gets the first element of an iterable.static <E> intFinds the index of the first element matching the predicate.static <A,B> List <B> Transforms the collection with a function.static <A,B, C extends Collection<B>>
CTransforms the collection with a function, storing the results in a target collection.static <A> Optional<A> Gets the single element of an iterable.
-
Constructor Details
-
CollectionUtil
public CollectionUtil()
-
-
Method Details
-
find
Finds the first element matching the predicate.- Type Parameters:
E- the element type- Parameters:
collection- the collection to be searchedfilter- the predicate to be matched- Returns:
- the first matching element, or empty if none match
-
indexOf
Finds the index of the first element matching the predicate.- Type Parameters:
E- the element type- Parameters:
list- the list to be searchedfilter- 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 typeB- the target type- Parameters:
collection- the source collectiontransform- the transformation function- Returns:
- a mutable list with the transformed entries
-
mapTo
public static <A,B, C mapToC extends Collection<B>> (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 typeB- the target typeC- the target collection type- Parameters:
collection- the source collectiontarget- the target collectiontransform- the transformation function- Returns:
- the target collection
-
filter
Filters a list based on a predicate.- Type Parameters:
A- the element type- Parameters:
collection- the source collectionfilter- the filtering predicate- Returns:
- a mutable list with the filtered entries
-
single
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
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
-