Top Description Inners Fields Constructors Methods
org.apache.commons.collections4

public Class IterableUtils

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Collection, .Collections, .Comparator, .Iterator, .List, .Set, org.apache.commons.collections4.functors.EqualPredicate, org.apache.commons.collections4.iterators.LazyIteratorChain, .ReverseListIterator, .UniqueFilterIterator

Provides utility methods and decorators for Iterable instances.

Note

this util class has been designed for fail-fast argument checking.

Since
4.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
IterableUtils.UnmodifiableIterable<E>

Inner class to distinguish unmodifiable instances.

Field Summary

Modifier and TypeField and Description
pack-priv static final FluentIterable<E>
EMPTY_ITERABLE

An empty iterable.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <
the element type
E
>
Iterable<E>

Returns:

a bounded view on the specified iterable
boundedIterable
(final Iterable<E>
the iterable to limit, may not be null
iterable
,
final long
the maximum number of elements, must not be negative
maxSize
)

Returns a view of the given iterable that contains at most the given number of elements.

public static <
the element type
E
>
Iterable<E>

Returns:

a new iterable, combining the provided iterables
chainedIterable
(final Iterable<? extends E>
the first iterable, may not be null
a
,
final Iterable<? extends E>
the second iterable, may not be null
b
)

Combines two iterables into a single iterable.

public static <
the element type
E
>
Iterable<E>

Returns:

a new iterable, combining the provided iterables
chainedIterable
(final Iterable<? extends E>
the first iterable, may not be null
a
,
final Iterable<? extends E>
the second iterable, may not be null
b
,
final Iterable<? extends E>
the third iterable, may not be null
c
)

Combines three iterables into a single iterable.

public static <
the element type
E
>
Iterable<E>

Returns:

a new iterable, combining the provided iterables
chainedIterable
(final Iterable<? extends E>
the first iterable, may not be null
a
,
final Iterable<? extends E>
the second iterable, may not be null
b
,
final Iterable<? extends E>
the third iterable, may not be null
c
,
final Iterable<? extends E>
the fourth iterable, may not be null
d
)

Combines four iterables into a single iterable.

public static <
the element type
E
>
Iterable<E>

Returns:

a new iterable, combining the provided iterables
chainedIterable
(final Iterable<? extends E>...
the iterables to combine, may not be null
iterables
)

Combines the provided iterables into a single iterable.

pack-priv static void
checkNotNull(final Iterable<?>
the iterable to check
iterable
)

Fail-fast check for null arguments.

pack-priv static void
checkNotNull(final Iterable<?>...
the iterables to check
iterables
)

Fail-fast check for null arguments.

public static <
the element type
E
>
Iterable<E>

Returns:

a filtered view on the specified iterable
collatedIterable
(final Iterable<? extends E>
the first iterable, may not be null
a
,
final Iterable<? extends E>
the second iterable, may not be null
b
)

Combines the two provided iterables into an ordered iterable using natural ordering.

public static <
the element type
E
>
Iterable<E>

Returns:

a filtered view on the specified iterable
collatedIterable
(final Comparator<? super E>
the comparator defining an ordering over the elements, may be null, in which case natural ordering will be used
comparator
,
final Iterable<? extends E>
the first iterable, may not be null
a
,
final Iterable<? extends E>
the second iterable, may not be null
b
)

Combines the two provided iterables into an ordered iterable using the provided comparator.

public static <
the type of object the Iterable contains
E
>
boolean

Returns:

true if the object is contained in the iterable, false otherwise
contains
(final Iterable<E>
the iterable to check, may be null
iterable
,
final Object
the object to check
object
)

Checks if the object is contained in the given iterable.

public static <
the type of object the Iterable contains
E
>
boolean

Returns:

true if the object is contained in the iterable, false otherwise
contains
(final Iterable<? extends E>
the iterable to check, may be null
iterable
,
final E
the object to check
object
,
final Equator<? super E>
the equator to use to check, may not be null
equator
)

Checks if the object is contained in the given iterable.

public static <
the type of object the Iterable contains
E
>
long

Returns:

the number of matches for the predicate in the collection
countMatches
(final Iterable<E>
the Iterable to get the input from, may be null
input
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Counts the number of elements in the input iterable that match the predicate.

public static <
the element type
E
>
Iterable<E>

Returns:

an empty iterable if the argument is null
emptyIfNull
(final Iterable<E>
the iterable, may be null
iterable
)

Returns an immutable empty iterable if the argument is null, or the argument itself otherwise.

public static <
the element type
E
>
Iterable<E>

Returns:

an empty iterable
emptyIterable
()

Gets an empty iterable.

private static <
the element type
E
>
Iterator<E>

Returns:

an empty iterator if the argument is null
emptyIteratorIfNull
(final Iterable<E>
the iterable, possibly null
iterable
)

Returns an empty iterator if the argument is null, or iterable.iterator() otherwise.

public static <
the element type
E
>
Iterable<E>

Returns:

a filtered view on the specified iterable
filteredIterable
(final Iterable<E>
the iterable to filter, may not be null
iterable
,
final Predicate<? super E>
the predicate used to filter elements, may not be null
predicate
)

Returns a view of the given iterable that only contains elements matching the provided predicate.

public static <
the element type
E
>
E

Returns:

the first element of the iterable which matches the predicate or null if none could be found
find
(final Iterable<E>
the iterable to search, may be null
iterable
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Finds the first element in the given iterable which matches the given predicate.

public static <
the type of object in the Iterable.
T
>
T

Returns:

the first object
first
(final Iterable<T>
the Iterable to get a value from, may be null
iterable
)

Shortcut for get(iterator, 0).

public static <
the element type
E
>
void
forEach(final Iterable<E>
the iterator to use, may be null
iterable
,
final Closure<? super E>
the closure to apply to each element, may not be null
closure
)

Applies the closure to each element of the provided iterable.

public static <
the type of object the Iterable contains
E
>
E

Returns:

the last element in the iterable, or null if iterable is null or empty
forEachButLast
(final Iterable<E>
the iterable to get the input from, may be null
iterable
,
final Closure<? super E>
the closure to perform, may not be null
closure
)

Executes the given closure on each but the last element in the iterable.

public static <
the element type that the Iterable may contain
E
,
the element type of the object to find
T extends E
>
int

Returns:

the number of occurrences of obj in iterable
frequency
(final Iterable<E>
the Iterable to search
iterable
,
final T
the object to find the cardinality of
obj
)

Returns the number of occurrences of the provided object in the iterable.

public static <
the type of object in the Iterable.
T
>
T

Returns:

the object at the specified index
get
(final Iterable<T>
the Iterable to get a value from, may be null
iterable
,
final int
the index to get
index
)

Returns the index-th value in the iterable's Iterator, throwing IndexOutOfBoundsException if there is no such element.

public static <
the element type
E
>
int

Returns:

the index of the first element which matches the predicate or -1 if none matches
indexOf
(final Iterable<E>
the iterable to search, may be null
iterable
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Returns the index of the first element in the specified iterable that matches the given predicate.

public static boolean

Returns:

true if the iterable is null or empty, false otherwise
isEmpty
(final Iterable<?>
the to use, may be null
iterable
)

Answers true if the provided iterable is empty.

public static <
the element type
E
>
Iterable<E>

Returns:

a view of the iterable, providing an infinite loop over its elements
loopingIterable
(final Iterable<E>
the iterable to loop, may not be null
iterable
)

Returns a view of the given iterable which will cycle infinitely over its elements.

public static <
the type of object the Iterable contains
E
>
boolean

Returns:

true if every element of the collection matches the predicate or if the collection is empty, false otherwise
matchesAll
(final Iterable<E>
the Iterable to use, may be null
iterable
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Answers true if a predicate is true for every element of an iterable.

public static <
the type of object the Iterable contains
E
>
boolean

Returns:

true if any element of the collection matches the predicate, false otherwise
matchesAny
(final Iterable<E>
the Iterable to use, may be null
iterable
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Answers true if a predicate is true for any element of the iterable.

public static <
the type of object the Iterable contains
O
>
List<List<O>>

Returns:

a list containing the output collections
partition
(final Iterable<? extends O>
the iterable to partition, may be null
iterable
,
final Predicate<? super O>
the predicate to use, may not be null
predicate
)

Partitions all elements from iterable into separate output collections, based on the evaluation of the given predicate.

public static <
the type of object the Iterable contains
O
>
List<List<O>>

Returns:

a list containing the output collections
partition
(final Iterable<? extends O>
the collection to get the input from, may be null
iterable
,
final Predicate<? super O>...
the predicates to use, may not be null
predicates
)

Partitions all elements from iterable into separate output collections, based on the evaluation of the given predicates.

public static <
the type of object the Iterable contains
O
,
the type of the output Collection
R extends Collection<O>
>
List<R>

Returns:

a list containing the output collections
partition
(final Iterable<? extends O>
the collection to get the input from, may be null
iterable
,
final Factory<R>
the factory used to create the output collections
partitionFactory
,
final Predicate<? super O>...
the predicates to use, may not be null
predicates
)

Partitions all elements from iterable into separate output collections, based on the evaluation of the given predicates.

public static <
the element type
E
>
Iterable<E>

Returns:

a reversed view of the specified iterable
reversedIterable
(final Iterable<E>
the iterable to use, may not be null
iterable
)

Returns a reversed view of the given iterable.

public static int

Returns:

the number of elements contained in the iterable
size
(final Iterable<?>
the iterable to check, may be null
iterable
)

Returns the number of elements contained in the given iterator.

public static <
the element type
E
>
Iterable<E>

Returns:

a view of the specified iterable, skipping the first N elements
skippingIterable
(final Iterable<E>
the iterable to use, may not be null
iterable
,
final long
the number of elements to skip from the start, must not be negative
elementsToSkip
)

Returns a view of the given iterable that skips the first N elements.

public static <
the element type
E
>
List<E>

Returns:

a list of the iterator contents
toList
(final Iterable<E>
the iterable to use, may be null
iterable
)

Gets a new list with the contents of the provided iterable.

public static <
the element type
E
>
String

Returns:

a string representation of iterable
toString
(final Iterable<E>
the iterable to convert to a string, may be null
iterable
)

Returns a string representation of the elements of the specified iterable.

public static <
the element type
E
>
String

Returns:

a string representation of iterable
toString
(final Iterable<E>
the iterable to convert to a string, may be null
iterable
,
final Transformer<? super E, String>
the transformer used to get a string representation of an element
transformer
)

Returns a string representation of the elements of the specified iterable.

public static <
the element type
E
>
String

Returns:

a string representation of iterable
toString
(final Iterable<E>
the iterable to convert to a string, may be null
iterable
,
final Transformer<? super E, String>
the transformer used to get a string representation of an element
transformer
,
final String
the string to delimit elements
delimiter
,
final String
the prefix, prepended to the string representation
prefix
,
final String
the suffix, appended to the string representation
suffix
)

Returns a string representation of the elements of the specified iterable.

public static <
the input element type
I
,
the output element type
O
>
Iterable<O>

Returns:

a transformed view of the specified iterable
transformedIterable
(final Iterable<I>
the iterable to transform, may not be null
iterable
,
final Transformer<? super I, ? extends O>
the transformer, must not be null
transformer
)

Returns a transformed view of the given iterable where all of its elements have been transformed by the provided transformer.

public static <
the element type
E
>
Iterable<E>

Returns:

a unique view of the specified iterable
uniqueIterable
(final Iterable<E>
the iterable to use, may not be null
iterable
)

Returns a unique view of the given iterable.

public static <
the element type
E
>
Iterable<E>

Returns:

an unmodifiable view of the specified iterable
unmodifiableIterable
(final Iterable<E>
the iterable to use, may not be null
iterable
)

Returns an unmodifiable view of the given iterable.

public static <
the element type
E
>
Iterable<E>

Returns:

a new iterable, interleaving the provided iterables
zippingIterable
(final Iterable<? extends E>
the first iterable, may not be null
a
,
final Iterable<? extends E>
the second iterable, may not be null
b
)

Interleaves two iterables into a single iterable.

public static <
the element type
E
>
Iterable<E>

Returns:

a new iterable, interleaving the provided iterables
zippingIterable
(final Iterable<? extends E>
the first iterable, may not be null
first
,
final Iterable<? extends E>...
the array of iterables to interleave, may not be null
others
)

Interleaves two iterables into a single iterable.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait