Top Description Inners Constructors Methods
com.google.common.collect

public final Class Iterables

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.collect.CollectPreconditions.checkRemove

An assortment of mainly legacy static utility methods that operate on or return objects of type Iterable. Except as noted, each method has a corresponding Iterator-based method in the Iterators class.

Java 8 users: several common uses for this class are now more comprehensively addressed by the new java.util.stream.Stream library. Read the method documentation below for comparisons. This class is not being deprecated, but we gently encourage you to migrate to streams.

Performance notes: Unless otherwise noted, all of the iterables produced in this class are lazy, which means that their iterators only advance the backing iteration when absolutely necessary.

See the Guava User Guide article on Iterables.

Authors
Kevin Bourrillion, Jared Levy
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <T extends @Nullable Object> boolean

Returns:

true if collection was modified as a result of this operation.
addAll
(Collection<T> addTo, Iterable<? extends T> elementsToAdd)

Adds all elements in iterable to collection.

public static <T extends @Nullable Object> boolean
all(Iterable<T> iterable, Predicate<? super T> predicate)

Returns true if every element in iterable satisfies the predicate.

public static <T extends @Nullable Object> boolean
any(Iterable<T> iterable, Predicate<? super T> predicate)

Returns true if any element in iterable satisfies the predicate.

private static <E extends @Nullable Object> Collection<E>
castOrCopyToCollection(Iterable<E> iterable)

Converts an iterable into a collection.

public static <T extends @Nullable Object> Iterable<T>
concat(Iterable<? extends T> a, Iterable<? extends T> b)

Combines two iterables into a single iterable.

public static <T extends @Nullable Object> Iterable<T>
concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c)

Combines three iterables into a single iterable.

public static <T extends @Nullable Object> Iterable<T>
concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c, Iterable<? extends T> d)

Combines four iterables into a single iterable.

public static <T extends @Nullable Object> Iterable<T>
concat(Iterable<? extends T>... inputs)

Combines multiple iterables into a single iterable.

public static <T extends @Nullable Object> Iterable<T>
concat(Iterable<? extends Iterable<? extends T>> inputs)

Combines multiple iterables into a single iterable.

public static <T extends @Nullable Object> Iterable<T>

Returns:

a view of the supplied iterable that wraps each generated iterator through Iterators#consumingIterator(Iterator); for queues, an iterable that generates iterators that return and consume the queue's elements in queue order
consumingIterable
(final Iterable<T>
the iterable to wrap
iterable
)

Returns a view of the supplied iterable that wraps each generated Iterator through Iterators#consumingIterator(Iterator).

public static boolean
contains(Iterable<? extends @Nullable Object> iterable, Object element)

Returns true if iterable contains any element o for which Objects.equals(o, element) would return true.

public static <T extends @Nullable Object> Iterable<T>
cycle(final Iterable<T> iterable)

Returns an iterable whose iterators cycle indefinitely over the elements of iterable.

public static <T extends @Nullable Object> Iterable<T>
cycle(T... elements)

Returns an iterable whose iterators cycle indefinitely over the provided elements.

public static boolean
elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2)

Determines whether two iterables contain equal elements in the same order.

public static <T extends @Nullable Object> Iterable<T>
filter(final Iterable<T> unfiltered, final Predicate<? super T> retainIfTrue)

Returns a view of unfiltered containing all elements that satisfy the input predicate retainIfTrue.

public static <T> Iterable<T>
filter(final Iterable<?> unfiltered, final Class<T> desiredType)

Returns a view of unfiltered containing all elements that are of the type desiredType.

public static <T extends @Nullable Object> T
find(Iterable<T> iterable, Predicate<? super T> predicate)

Returns the first element in iterable that satisfies the given predicate; use this method only when such an element is known to exist.

public static <T extends @Nullable Object> T
find(Iterable<? extends T> iterable, Predicate<? super T> predicate, T defaultValue)

Returns the first element in iterable that satisfies the given predicate, or defaultValue if none found.

public static int
frequency(Iterable<?> iterable, Object element)

Returns the number of elements in the specified iterable that equal the specified object.

public static <T extends @Nullable Object> T

Returns:

the element at the specified position in iterable
get
(Iterable<T> iterable, int
position of the element to return
position
)

Returns the element at the specified position in an iterable.

public static <T extends @Nullable Object> T

Returns:

the element at the specified position in iterable or defaultValue if iterable contains fewer than position + 1 elements.
get
(Iterable<? extends T> iterable, int
position of the element to return
position
,
T
the default value to return if position is greater than or equal to the size of the iterable
defaultValue
)

Returns the element at the specified position in an iterable or a default value otherwise.

public static <T extends @Nullable Object> T

Returns:

the first element of iterable or the default value
getFirst
(Iterable<? extends T> iterable, T
the default value to return if the iterable is empty
defaultValue
)

Returns the first element in iterable or defaultValue if the iterable is empty.

public static <T extends @Nullable Object> T

Returns:

the last element of iterable
getLast
(Iterable<T> iterable)

Returns the last element of iterable.

public static <T extends @Nullable Object> T

Returns:

the last element of iterable or the default value
getLast
(Iterable<? extends T> iterable, T
the value to return if iterable is empty
defaultValue
)

Returns the last element of iterable or defaultValue if the iterable is empty.

private static <T extends @Nullable Object> T
public static <T extends @Nullable Object> T
getOnlyElement(Iterable<T> iterable)

Returns the single element contained in iterable.

public static <T extends @Nullable Object> T
getOnlyElement(Iterable<? extends T> iterable, T defaultValue)

Returns the single element contained in iterable, or defaultValue if the iterable is empty.

public static <T extends @Nullable Object> int
indexOf(Iterable<T> iterable, Predicate<? super T> predicate)

Returns the index in iterable of the first element that satisfies the provided predicate, or -1 if the Iterable has no such elements.

public static boolean

Returns:

true if the iterable contains no elements
isEmpty
(Iterable<?> iterable)

Determines if the given iterable contains no elements.

public static <T extends @Nullable Object> Iterable<T>
limit(final Iterable<T>
the iterable to limit
iterable
,
final int
the maximum number of elements in the returned iterable
limitSize
)

Returns a view of iterable containing its first limitSize elements.

public static <T extends @Nullable Object> Iterable<T>
mergeSorted(final Iterable<? extends Iterable<? extends T>> iterables, final Comparator<? super T> comparator)

Returns an iterable over the merged contents of all given iterables.

public static <T extends @Nullable Object> Iterable<List<@Nullable T>>

Returns:

an iterable of unmodifiable lists containing the elements of iterable divided into partitions (the final iterable may have trailing null elements)
paddedPartition
(final Iterable<T>
the iterable to return a partitioned view of
iterable
,
final int
the desired size of each partition
size
)

Divides an iterable into unmodifiable sublists of the given size, padding the final iterable with null values if necessary.

public static <T extends @Nullable Object> Iterable<List<T>>

Returns:

an iterable of unmodifiable lists containing the elements of iterable divided into partitions
partition
(final Iterable<T>
the iterable to return a partitioned view of
iterable
,
final int
the desired size of each partition (the last may be smaller)
size
)

Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller).

public static boolean

Returns:

true if any element was removed from iterable
removeAll
(Iterable<?>
the iterable to (potentially) remove elements from
removeFrom
,
Collection<?>
the elements to remove
elementsToRemove
)

Removes, from an iterable, every element that belongs to the provided collection.

pack-priv static <T extends @Nullable Object> T
removeFirstMatching(Iterable<T> removeFrom, Predicate<? super T> predicate)

Removes and returns the first matching element, or returns null if there is none.

public static <T extends @Nullable Object> boolean

Returns:

true if any elements were removed from the iterable
removeIf
(Iterable<T>
the iterable to (potentially) remove elements from
removeFrom
,
Predicate<? super T>
a predicate that determines whether an element should be removed
predicate
)

Removes, from an iterable, every element that satisfies the provided predicate.

public static boolean

Returns:

true if any element was removed from iterable
retainAll
(Iterable<?>
the iterable to (potentially) remove elements from
removeFrom
,
Collection<?>
the elements to retain
elementsToRetain
)

Removes, from an iterable, every element that does not belong to the provided collection.

public static int
size(Iterable<?> iterable)

Returns the number of elements in iterable.

public static <T extends @Nullable Object> Iterable<T>
skip(final Iterable<T> iterable, final int numberToSkip)

Returns a view of iterable that skips its first numberToSkip elements.

public static <T extends @Nullable Object> T[]

Returns:

a newly-allocated array into which all the elements of the iterable have been copied
toArray
(Iterable<? extends T>
the iterable to copy
iterable
,
Class<@NonNull T>
the type of the elements
type
)

Copies an iterable's elements into an array.

pack-priv static <T extends @Nullable Object> T[]
toArray(Iterable<? extends T> iterable, T[] array)

pack-priv static @Nullable Object[]

Returns:

a newly-allocated array into which all the elements of the iterable have been copied
toArray
(Iterable<?>
the iterable to copy
iterable
)

Copies an iterable's elements into an array.

public static String
toString(Iterable<?> iterable)

Returns a string representation of iterable, with the format [e1, e2, ..., en] (that is, identical to Arrays.toString(Iterables.toArray(iterable))).

public static <F extends @Nullable Object, T extends @Nullable Object> Iterable<T>
transform(final Iterable<F> fromIterable, final Function<? super F, ? extends T> function)

Returns a view containing the result of applying function to each element of fromIterable.

public static <T> Optional<T>
tryFind(Iterable<T> iterable, Predicate<? super T> predicate)

Returns an Optional containing the first element in iterable that satisfies the given predicate, if such an element exists.

public static <T extends @Nullable Object> Iterable<T>
unmodifiableIterable(final Iterable<? extends T> iterable)

Returns an unmodifiable view of iterable.

public static <E> Iterable<E>
unmodifiableIterable(ImmutableCollection<E> iterable)

Deprecated no need to use this
Simply returns its argument.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait