Class XIterableUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRepresents an item with an index. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanSimilar to thematchesAllmethod from Apache Commons, but does not throw on null predicates.static <T> booleanSimilar to thematchesAnymethod from Apache Commons, but does not throw on null predicates.static <T,A, C> C collectIterable(Iterable<? extends T> iterable, Collector<? super T, A, ? extends C> collector) Collects the elements of an iterable using a collector.static <T> Iterable<T> filteredIterable(Iterable<? extends T> iterable, Predicate<? super T> test) Removes all elements of the given iterable that do not match the given mapper predicate.static <I,O> Iterable <O> filteredMappedIterable(Iterable<? extends I> iterable, Function<? super I, ? extends O> mapper, Predicate<? super O> test) CombinesmappedIterableandfilteredIterableinto a single method for convenience.static <T> TfirstOrNull(Iterable<T> iterable) Similar to thefirstmethod from Apache Commons, but returnsnullinstead of throwing if the iterable is empty.static <In,Out> Iterable <Out> flatMappedIterable(Iterable<? extends In> iterable, Function<? super In, ? extends Iterable<? extends Out>> mapper) Maps all elements of the given iterable using the given mapper function.static <In,Out> Iterator <Out> flatMappedIterator(Iterator<? extends In> iterator, Function<? super In, ? extends Iterator<? extends Out>> mapper) Maps all elements of the given iterator using the given mapper function.static <T> Collection<T> iterableAsCollection(Iterable<T> iterable) Similar toiterableToCollection(Iterable), but returns the given iterable unchanged if already a collection.static <T> List<T> iterableAsList(Iterable<T> iterable) Similar toiterableToList(Iterable), but returns the given iterable unchanged if already a list.static <T> Set<T> iterableAsSet(Iterable<T> iterable) Similar toiterableToSet(Iterable), but returns the given iterable unchanged if already a set.static <T> Collection<T> iterableToCollection(Iterable<? extends T> iterable) Collects the elements of an iterable into a collection.static <T, S extends Collection<T>>
SiterableToCollection(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> collectionSupplier) static <T, L extends Collection<T>>
LiterableToCollection(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends L> collectionCollector) Collects the elements of an iterable into a collection.static <T> List<T> iterableToList(Iterable<? extends T> iterable) Collects the elements of an iterable into a list.static <T, S extends List<T>>
SiterableToList(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> listSupplier) static <T, L extends List<T>>
LiterableToList(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends L> listCollector) Collects the elements of an iterable into a list.static <T> Set<T> iterableToSet(Iterable<? extends T> iterable) Collects the elements of an iterable into a set.static <T, S extends Set<T>>
SiterableToSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier) static <T, S extends Set<T>>
SiterableToSet(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends S> setCollector) static <I,O> Iterable <O> mappedFilteredIterable(Iterable<? extends I> iterable, Predicate<? super I> test, Function<? super I, ? extends O> mapper) CombinesfilteredIterableandmappedIterableinto a single method for convenience.static <In,Out> Iterable <Out> mappedIterable(Iterable<? extends In> iterable, Function<? super In, ? extends Out> mapper) Maps all elements of the given iterable using the given mapper function.static <In,Out> Iterator <Out> mappedIterator(Iterator<? extends In> iterator, Function<? super In, ? extends Out> mapper) Maps all elements of the given iterator using the given mapper function.static <T> booleanSimilar to thematchesNonemethod from Apache Commons, but does not throw on null predicates.static <T> Iterable<T> retainWhile(Iterable<? extends T> iterable, Predicate<? super T> test) Applies the given test predicate to the elements of the iterable.static <T> Set<T> Deprecated.static <T, S extends Set<T>>
SDeprecated.UseiterableToList(Iterable, Supplier)instead.static <T, S extends Set<T>>
SDeprecated.UseiterableToSet(Iterable, Collector)instead.static <T> Iterable<XIterableUtils.Indexed<T>> Wraps an iterable and adds the index to each element.static <T> Stream<XIterableUtils.Indexed<T>> withIndexStreamed(Iterable<T> iterable) Wraps an iterable and adds the index to each element.
-
Constructor Details
-
XIterableUtils
public XIterableUtils()
-
-
Method Details
-
allMatch
Similar to thematchesAllmethod from Apache Commons, but does not throw on null predicates. Returnstrueif all items match the given predicate. Returnstrueif the iterable is null or empty. A null predicate is treated as a predicate that always returnsfalse.- Type Parameters:
T- Type of the items in the iterable.- Parameters:
items- Items to check.predicate- Predicate to check.- Returns:
trueif no item exists which does not match the predicate.
-
anyMatch
Similar to thematchesAnymethod from Apache Commons, but does not throw on null predicates. Returnstrueif at least one item exists which matches the predicate. Returnsfalseif the iterable is null or empty. A null predicate is treated as a predicate that always returnsfalse.- Type Parameters:
T- Type of the items in the iterable.- Parameters:
items- Items to check.predicate- Predicate to check.- Returns:
trueif at least one item exists which matches the predicate.
-
collectIterable
public static <T,A, C collectIterableC> (Iterable<? extends T> iterable, Collector<? super T, A, ? extends C> collector) Collects the elements of an iterable using a collector. This behaves the sames asstreamIterable(emptyIterableIfNull(iterable)).collect(collector);but is more concise to write.- Type Parameters:
T- Type of the items.A- Type of the accumulator.C- Type of the resulting container.- Parameters:
iterable- Iterable to collect.collector- Collector to use.- Returns:
- The result of the collection.
-
filteredIterable
public static <T> Iterable<T> filteredIterable(Iterable<? extends T> iterable, Predicate<? super T> test) Removes all elements of the given iterable that do not match the given mapper predicate. The returned iterable will contain only the elements that match the predicate.Compared with
filteredIterablefrom Apache Commons andIterables.filterfrom Guava, this method (a) returns an iterable that does not allow the elements to be modified; (b) allows the iterable to be null; (c) uses the standardjava.util.function.Predicateinterface instead of the custom interfaces from the other libraries.- Type Parameters:
T- Type of the input elements.- Parameters:
iterable- Iterable to map. Null is treated as an empty iterable.test- Test predicate to apply to each element. null is treated as a predicate that returns false for all inputs.- Returns:
- An iterable with the filtered elements.
-
filteredMappedIterable
public static <I,O> Iterable<O> filteredMappedIterable(Iterable<? extends I> iterable, Function<? super I, ? extends O> mapper, Predicate<? super O> test) CombinesmappedIterableandfilteredIterableinto a single method for convenience.Applies the mapper to each element of the given iterable, then removes all elements that do not match the test predicate. The returned iterable will contain only the elements that match the predicate.
Compared with
filteredIterableandmappedIterablefrom Apache Commons andIterables.filterfrom Guava, this method (a) returns an iterable that does not allow the elements to be modified; (b) allows the iterable to be null; (c) uses the standardjava.util.function.Predicateinterface instead of the custom interfaces from the other libraries.- Type Parameters:
I- Type of the provided input elements.O- Type of the mapped output elements.- Parameters:
iterable- Iterable to map. Null is treated as an empty iterable.mapper- Mapper function to apply to each element. null is treated as a function that returns null for all inputs.test- Test predicate to apply to each input element. null is treated as a predicate that returns false for all inputs.- Returns:
- An iterable with the mapped and filtered elements.
-
firstOrNull
Similar to thefirstmethod from Apache Commons, but returnsnullinstead of throwing if the iterable is empty.- Type Parameters:
T- Type of the items in the iterable.- Parameters:
iterable- Iterable to process.- Returns:
- The first item in the iterable, or
nullif either the iterable isnullor it contains no items.
-
flatMappedIterable
public static <In,Out> Iterable<Out> flatMappedIterable(Iterable<? extends In> iterable, Function<? super In, ? extends Iterable<? extends Out>> mapper) Maps all elements of the given iterable using the given mapper function. The returned iterable will contain the mapped elements. Each output of the given mapper is anIterable, this method iterates over these elements.- Type Parameters:
In- Type of the input elements.Out- Type of the output elements.- Parameters:
iterable- Iterable to map. Null is treated as an empty iterable.mapper- Mapper function to apply to each element. null is treated as a function that returns null for all inputs.- Returns:
- An iterable with the mapped elements.
-
flatMappedIterator
public static <In,Out> Iterator<Out> flatMappedIterator(Iterator<? extends In> iterator, Function<? super In, ? extends Iterator<? extends Out>> mapper) Maps all elements of the given iterator using the given mapper function. The returned iterator will contain the mapped elements. Each output of the given mapper is anIterator, this method iterates over these elements.- Type Parameters:
In- Type of the input elements.Out- Type of the output elements.- Parameters:
iterator- Iterator to map. Null is treated as an empty iterator.mapper- Mapper function to apply to each element. null is treated as a function that returns null for all inputs.- Returns:
- An iterator with the mapped elements.
-
iterableAsCollection
Similar toiterableToCollection(Iterable), but returns the given iterable unchanged if already a collection.iterableToCollection(Iterable)always returns a new collection.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A collection with the given elements.
-
iterableAsList
Similar toiterableToList(Iterable), but returns the given iterable unchanged if already a list.iterableToList(Iterable)always returns a new list.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A list with the given elements.
-
iterableAsSet
Similar toiterableToSet(Iterable), but returns the given iterable unchanged if already a set.iterableToSet(Iterable)always returns a new set.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A set with the given elements.
-
iterableToCollection
public static <T, S extends Collection<T>> S iterableToCollection(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> collectionSupplier) - Type Parameters:
T- Type of the items.S- Type of the collection.- Parameters:
iterable- Iterable to convert.collectionSupplier- Supplier for the type of collection implementation to use.- Returns:
- A collection with the given elements.
-
iterableToCollection
Collects the elements of an iterable into a collection. No guarantees are made regarding the type of the collection. Use one of the other methods if you need a specific type.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A collection with the given elements.
-
iterableToCollection
public static <T, L extends Collection<T>> L iterableToCollection(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends L> collectionCollector) Collects the elements of an iterable into a collection.- Type Parameters:
T- Type of the items.L- Type of the collection.- Parameters:
iterable- Iterable to convert.collectionCollector- Collector for the collection implementation to use.- Returns:
- A collection with the given elements.
-
iterableToList
Collects the elements of an iterable into a list. No guarantees are made regarding the type of the list. Use one of the other methods if you need a specific type.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A list with the given elements.
-
iterableToList
public static <T, L extends List<T>> L iterableToList(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends L> listCollector) Collects the elements of an iterable into a list.- Type Parameters:
T- Type of the items.L- Type of the list.- Parameters:
iterable- Iterable to convert.listCollector- Collector for the list implementation to use.- Returns:
- A list with the given elements.
-
iterableToList
public static <T, S extends List<T>> S iterableToList(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> listSupplier) - Type Parameters:
T- Type of the items.S- Type of the list.- Parameters:
iterable- Iterable to convert.listSupplier- Supplier for the type of list implementation to use.- Returns:
- A list with the given elements.
-
iterableToSet
Collects the elements of an iterable into a set. No guarantees are made regarding the type of the list. Use one of the other methods if you need a specific type.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A set with the given elements.
-
iterableToSet
public static <T, S extends Set<T>> S iterableToSet(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends S> setCollector) - Type Parameters:
T- Type of the items.S- Type of the set.- Parameters:
iterable- Iterable to convert.setCollector- Collector for the type of set implementation to use.- Returns:
- A set with the given elements.
-
iterableToSet
public static <T, S extends Set<T>> S iterableToSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier) - Type Parameters:
T- Type of the items.S- Type of the set.- Parameters:
iterable- Iterable to convert.setSupplier- Supplier for the type of set implementation to use.- Returns:
- A set with the given elements.
-
mappedFilteredIterable
public static <I,O> Iterable<O> mappedFilteredIterable(Iterable<? extends I> iterable, Predicate<? super I> test, Function<? super I, ? extends O> mapper) CombinesfilteredIterableandmappedIterableinto a single method for convenience.Removes all elements of the given iterable that do not match the given test predicate, then apply the mapper function. The returned iterable will contain only the elements that match the predicate.
Compared with
filteredIterableandmappedIterablefrom Apache Commons andIterables.filterfrom Guava, this method (a) returns an iterable that does not allow the elements to be modified; (b) allows the iterable to be null; (c) uses the standardjava.util.function.Predicateinterface instead of the custom interfaces from the other libraries.- Type Parameters:
I- Type of the input elements.O- Type of the output elements.- Parameters:
iterable- Iterable to map. Null is treated as an empty iterable.test- Test predicate to apply to each mapped element. null is treated as a predicate that returns false for all inputs.mapper- Mapper function to apply to each input element. null is treated as a function that returns null for all inputs.- Returns:
- An iterable with the filtered and mapped elements.
-
mappedIterable
public static <In,Out> Iterable<Out> mappedIterable(Iterable<? extends In> iterable, Function<? super In, ? extends Out> mapper) Maps all elements of the given iterable using the given mapper function. The returned iterable will contain the mapped elements.Compared with
transformedIterablefrom Apache Commons andIterables.transformfrom Guava, this method (a) returns an iterable that does not allow the elements to be modified; (b) allows the iterable to be null; (c) uses the standardjava.util.function.Functioninterface instead of the custom interfaces from the other libraries.- Type Parameters:
In- Type of the input elements.Out- Type of the output elements.- Parameters:
iterable- Iterable to map. Null is treated as an empty iterable.mapper- Mapper function to apply to each element. null is treated as a function that returns null for all inputs.- Returns:
- An iterable with the mapped elements.
-
mappedIterator
public static <In,Out> Iterator<Out> mappedIterator(Iterator<? extends In> iterator, Function<? super In, ? extends Out> mapper) Maps all elements of the given iterator using the given mapper function. The returned iterator will contain the mapped elements.Compared with
transformedIteratorfrom Apache Commons andIterators.transformfrom Guava, this method (a) returns an iterator that does not allow the elements to be modified; (b) allows the iterator to be null; (c) uses the standardjava.util.function.Functioninterface instead of the custom interfaces from the other libraries.- Type Parameters:
In- Type of the input elements.Out- Type of the output elements.- Parameters:
iterator- Iterator to map. Null is treated as an empty iterator.mapper- Mapper function to apply to each element. null is treated as a function that returns null for all inputs.- Returns:
- An iterator with the mapped elements.
-
noneMatch
Similar to thematchesNonemethod from Apache Commons, but does not throw on null predicates. Returnstrueif no item matches the given predicate. Returnstrueif the iterable is null or empty. A null predicate is treated as a predicate that always returnsfalse.- Type Parameters:
T- Type of the items in the iterable.- Parameters:
items- Items to check.predicate- Predicate to check.- Returns:
trueif no item exists which does not match the predicate.
-
retainWhile
public static <T> Iterable<T> retainWhile(Iterable<? extends T> iterable, Predicate<? super T> test) Applies the given test predicate to the elements of the iterable. Once the test predicate returns false, excludes all elements from that point on. The returned iterable will contain only the initial elements at the beginning that match the test predicate.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to process. Null is treated as an empty iterable.test- Test predicate to apply on the elements. Null is treated as a predicate that always returns false.- Returns:
- An iterable with all elements from the given iterable, but excluding all elements after the first element that does not match the test predicate.
-
toSet
Deprecated.UseiterableToSet(Iterable)instead.Collects the elements of an iterable into a set. No guarantees are made regarding the type of the list. Use one of the other methods if you need a specific type.- Type Parameters:
T- Type of the items.- Parameters:
iterable- Iterable to convert.- Returns:
- A set with the given elements.
-
toSet
@Deprecated public static <T, S extends Set<T>> S toSet(Iterable<? extends T> iterable, Collector<? super T, ?, ? extends S> setCollector) Deprecated.UseiterableToSet(Iterable, Collector)instead.- Type Parameters:
T- Type of the items.S- Type of the set.- Parameters:
iterable- Iterable to convert.setCollector- Collector for the type of set implementation to use.- Returns:
- A set with the given elements.
-
toSet
@Deprecated public static <T, S extends Set<T>> S toSet(Iterable<? extends T> iterable, com.google.common.base.Supplier<? extends S> setSupplier) Deprecated.UseiterableToList(Iterable, Supplier)instead.- Type Parameters:
T- Type of the items.S- Type of the set.- Parameters:
iterable- Iterable to convert.setSupplier- Supplier for the type of set implementation to use.- Returns:
- A set with the given elements.
-
withIndex
Wraps an iterable and adds the index to each element.- Type Parameters:
T- Type of the elements in the iterable.- Parameters:
iterable- Iterable to wrap.- Returns:
- An iterable that iterates over the same elements, but with a 0-based index added to each element.
-
withIndexStreamed
Wraps an iterable and adds the index to each element.- Type Parameters:
T- Type of the elements in the iterable.- Parameters:
iterable- Iterable to wrap.- Returns:
- A stream that iterates over the same elements, but with a 0-based index added to each element.
-
iterableToSet(Iterable)instead.