|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.collect.Iterables
public final class Iterables
This class contains 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.
Method Summary | ||
---|---|---|
static
|
addAll(Collection<T> addTo,
Iterable<? extends T> elementsToAdd)
Adds all elements in iterable to collection . |
|
static
|
all(Iterable<T> iterable,
Predicate<? super T> predicate)
Returns true if every element in iterable satisfies the
predicate. |
|
static
|
any(Iterable<T> iterable,
Predicate<? super T> predicate)
Returns true if one or more elements in iterable satisfy
the predicate. |
|
static
|
concat(Iterable<? extends Iterable<? extends T>> inputs)
Combines multiple iterables into a single iterable. |
|
static
|
concat(Iterable<? extends T>... inputs)
Combines multiple iterables into a single iterable. |
|
static
|
concat(Iterable<? extends T> a,
Iterable<? extends T> b)
Combines two iterables into a single iterable. |
|
static
|
concat(Iterable<? extends T> a,
Iterable<? extends T> b,
Iterable<? extends T> c)
Combines three iterables into a single iterable. |
|
static
|
concat(Iterable<? extends T> a,
Iterable<? extends T> b,
Iterable<? extends T> c,
Iterable<? extends T> d)
Combines four iterables into a single iterable. |
|
static
|
consumingIterable(Iterable<T> iterable)
Returns a view of the supplied iterable that wraps each generated Iterator through Iterators.consumingIterator(Iterator) . |
|
static boolean |
contains(Iterable<?> iterable,
Object element)
Returns true if iterable contains element ; that is,
any object for while equals(element) is true. |
|
static
|
cycle(Iterable<T> iterable)
Returns an iterable whose iterators cycle indefinitely over the elements of iterable . |
|
static
|
cycle(T... elements)
Returns an iterable whose iterators cycle indefinitely over the provided elements. |
|
static boolean |
elementsEqual(Iterable<?> iterable1,
Iterable<?> iterable2)
Determines whether two iterables contain equal elements in the same order. |
|
static
|
filter(Iterable<?> unfiltered,
Class<T> type)
Returns all instances of class type in unfiltered . |
|
static
|
filter(Iterable<T> unfiltered,
Predicate<? super T> predicate)
Returns the elements of unfiltered that satisfy a predicate. |
|
static
|
find(Iterable<T> iterable,
Predicate<? super T> predicate)
Returns the first element in iterable that satisfies the given
predicate. |
|
static int |
frequency(Iterable<?> iterable,
Object element)
Returns the number of elements in the specified iterable that equal the specified object. |
|
static
|
get(Iterable<T> iterable,
int position)
Returns the element at the specified position in an iterable. |
|
static
|
getLast(Iterable<T> iterable)
Returns the last element of iterable . |
|
static
|
getOnlyElement(Iterable<T> iterable)
Returns the single element contained in iterable . |
|
static
|
getOnlyElement(Iterable<T> iterable,
T defaultValue)
Returns the single element contained in iterable , or defaultValue if the iterable is empty. |
|
static
|
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. |
|
static
|
isEmpty(Iterable<T> iterable)
Determines if the given iterable contains no elements. |
|
static
|
paddedPartition(Iterable<T> iterable,
int size)
Divides an iterable into unmodifiable sublists of the given size, padding the final iterable with null values if necessary. |
|
static
|
partition(Iterable<T> iterable,
int size)
Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller). |
|
static boolean |
removeAll(Iterable<?> removeFrom,
Collection<?> elementsToRemove)
Removes, from an iterable, every element that belongs to the provided collection. |
|
static
|
removeIf(Iterable<T> removeFrom,
Predicate<? super T> predicate)
Removes, from an iterable, every element that satisfies the provided predicate. |
|
static boolean |
retainAll(Iterable<?> removeFrom,
Collection<?> elementsToRetain)
Removes, from an iterable, every element that does not belong to the provided collection. |
|
static
|
reverse(List<T> list)
Adapts a list to an iterable with reversed iteration order. |
|
static int |
size(Iterable<?> iterable)
Returns the number of elements in iterable . |
|
static
|
toArray(Iterable<? extends T> iterable,
Class<T> type)
Copies an iterable's elements into an array. |
|
static String |
toString(Iterable<?> iterable)
Returns a string representation of iterable , with the format
[e1, e2, ..., en] . |
|
static
|
transform(Iterable<F> fromIterable,
Function<? super F,? extends T> function)
Returns an iterable that applies function to each element of fromIterable . |
|
static
|
unmodifiableIterable(Iterable<T> iterable)
Returns an unmodifiable view of iterable . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static <T> Iterable<T> unmodifiableIterable(Iterable<T> iterable)
iterable
.
public static int size(Iterable<?> iterable)
iterable
.
public static boolean contains(Iterable<?> iterable, @Nullable Object element)
true
if iterable
contains element
; that is,
any object for while equals(element)
is true.
public static boolean removeAll(Iterable<?> removeFrom, Collection<?> elementsToRemove)
This method calls Collection.removeAll(java.util.Collection>)
if iterable
is a
collection, and Iterators.removeAll(java.util.Iterator>, java.util.Collection>)
otherwise.
removeFrom
- the iterable to (potentially) remove elements fromelementsToRemove
- the elements to remove
true
if any elements are removed from iterable
public static boolean retainAll(Iterable<?> removeFrom, Collection<?> elementsToRetain)
This method calls Collection.retainAll(java.util.Collection>)
if iterable
is a
collection, and Iterators.retainAll(java.util.Iterator>, java.util.Collection>)
otherwise.
removeFrom
- the iterable to (potentially) remove elements fromelementsToRetain
- the elements to retain
true
if any elements are removed from iterable
public static <T> boolean removeIf(Iterable<T> removeFrom, Predicate<? super T> predicate)
removeFrom
- the iterable to (potentially) remove elements frompredicate
- a predicate that determines whether an element should
be removed
true
if any elements were removed from the iterable
UnsupportedOperationException
- if the iterable does not support
remove()
.public static boolean elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2)
true
if iterable1
and iterable2
contain the same number of elements and every element
of iterable1
is equal to the corresponding element of
iterable2
.
public static String toString(Iterable<?> iterable)
iterable
, with the format
[e1, e2, ..., en]
.
public static <T> T getOnlyElement(Iterable<T> iterable)
iterable
.
NoSuchElementException
- if the iterable is empty
IllegalArgumentException
- if the iterable contains multiple
elementspublic static <T> T getOnlyElement(Iterable<T> iterable, @Nullable T defaultValue)
iterable
, or defaultValue
if the iterable is empty.
IllegalArgumentException
- if the iterator contains multiple
elementspublic static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type)
iterable
- the iterable to copytype
- the type of the elements
public static <T> boolean addAll(Collection<T> addTo, Iterable<? extends T> elementsToAdd)
iterable
to collection
.
true
if collection
was modified as a result of this
operation.public static int frequency(Iterable<?> iterable, @Nullable Object element)
Collections.frequency(java.util.Collection>, java.lang.Object)
public static <T> Iterable<T> cycle(Iterable<T> iterable)
iterable
.
That iterator supports remove()
if iterable.iterator()
does. After remove()
is called, subsequent cycles omit the removed
element, which is no longer in iterable
. The iterator's
hasNext()
method returns true
until iterable
is
empty.
Warning: Typical uses of the resulting iterator may produce an
infinite loop. You should use an explicit break
or be certain that
you will eventually remove all the elements.
To cycle over the iterable n
times, use the following:
Iterables.concat(Collections.nCopies(n, iterable))
public static <T> Iterable<T> cycle(T... elements)
After remove
is invoked on a generated iterator, the removed
element will no longer appear in either that iterator or any other iterator
created from the same source iterable. That is, this method behaves exactly
as Iterables.cycle(Lists.newArrayList(elements))
. The iterator's
hasNext
method returns true
until all of the original
elements have been removed.
Warning: Typical uses of the resulting iterator may produce an
infinite loop. You should use an explicit break
or be certain that
you will eventually remove all the elements.
To cycle over the elements n
times, use the following:
Iterables.concat(Collections.nCopies(n, Arrays.asList(elements)))
public static <T> Iterable<T> concat(Iterable<? extends T> a, Iterable<? extends T> b)
a
, followed by the elements
in b
. The source iterators are not polled until necessary.
The returned iterable's iterator supports remove()
when the
corresponding input iterator supports it.
public static <T> Iterable<T> concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c)
a
, followed by the
elements in b
, followed by the elements in c
. The source
iterators are not polled until necessary.
The returned iterable's iterator supports remove()
when the
corresponding input iterator supports it.
public static <T> Iterable<T> concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T> c, Iterable<? extends T> d)
a
, followed by the
elements in b
, followed by the elements in c
, followed by
the elements in d
. The source iterators are not polled until
necessary.
The returned iterable's iterator supports remove()
when the
corresponding input iterator supports it.
public static <T> Iterable<T> concat(Iterable<? extends T>... inputs)
inputs
. The input iterators are not polled until necessary.
The returned iterable's iterator supports remove()
when the
corresponding input iterator supports it.
NullPointerException
- if any of the provided iterables is nullpublic static <T> Iterable<T> concat(Iterable<? extends Iterable<? extends T>> inputs)
inputs
. The input iterators are not polled until necessary.
The returned iterable's iterator supports remove()
when the
corresponding input iterator supports it. The methods of the returned
iterable may throw NullPointerException
if any of the input
iterators are null.
public static <T> Iterable<List<T>> partition(Iterable<T> iterable, int size)
[a, b, c, d, e]
with a partition size of 3 yields [[a, b, c], [d, e]]
-- an outer iterable containing two inner lists of
three and two elements, all in the original order.
Iterators returned by the returned iterable do not support the Iterator.remove()
method. The returned lists implement RandomAccess
, whether or not the input list does.
Note: if iterable
is a List
, use Lists.partition(List, int)
instead.
iterable
- the iterable to return a partitioned view ofsize
- the desired size of each partition (the last may be smaller)
iterable
divided into partitions
IllegalArgumentException
- if size
is nonpositivepublic static <T> Iterable<List<T>> paddedPartition(Iterable<T> iterable, int size)
[a, b, c, d, e]
with a partition size of 3
yields [[a, b, c], [d, e, null]]
-- an outer iterable containing
two inner lists of three elements each, all in the original order.
Iterators returned by the returned iterable do not support the Iterator.remove()
method.
iterable
- the iterable to return a partitioned view ofsize
- the desired size of each partition
iterable
divided into partitions (the final iterable may have
trailing null elements)
IllegalArgumentException
- if size
is nonpositivepublic static <T> Iterable<T> filter(Iterable<T> unfiltered, Predicate<? super T> predicate)
unfiltered
that satisfy a predicate. The
resulting iterable's iterator does not support remove()
.
public static <T> Iterable<T> filter(Iterable<?> unfiltered, Class<T> type)
type
in unfiltered
. The
returned iterable has elements whose class is type
or a subclass of
type
. The returned iterable's iterator does not support
remove()
.
unfiltered
- an iterable containing objects of any typetype
- the type of elements desired
public static <T> boolean any(Iterable<T> iterable, Predicate<? super T> predicate)
true
if one or more elements in iterable
satisfy
the predicate.
public static <T> boolean all(Iterable<T> iterable, Predicate<? super T> predicate)
true
if every element in iterable
satisfies the
predicate. If iterable
is empty, true
is returned.
public static <T> T find(Iterable<T> iterable, Predicate<? super T> predicate)
iterable
that satisfies the given
predicate.
NoSuchElementException
- if no element in iterable
matches
the given predicatepublic static <T> int indexOf(Iterable<T> iterable, Predicate<? super T> predicate)
iterable
of the first element that satisfies
the provided predicate
, or -1
if the Iterable has no such
elements.
More formally, returns the lowest index i
such that
predicate.apply(Iterables.get(iterable, i))
is true
or
-1
if there is no such index.
public static <F,T> Iterable<T> transform(Iterable<F> fromIterable, Function<? super F,? extends T> function)
function
to each element of fromIterable
.
The returned iterable's iterator supports remove()
if the
provided iterator does. After a successful remove()
call,
fromIterable
no longer contains the corresponding element.
public static <T> T get(Iterable<T> iterable, int position)
position
- position of the element to return
iterable
IndexOutOfBoundsException
- if position
is negative or
greater than or equal to the size of iterable
public static <T> T getLast(Iterable<T> iterable)
iterable
.
iterable
NoSuchElementException
- if the iterable has no elementspublic static <T> Iterable<T> consumingIterable(Iterable<T> iterable)
Iterator
through Iterators.consumingIterator(Iterator)
.
iterable
- the iterable to wrap
Iterators.consumingIterator(Iterator)
Iterators.consumingIterator(Iterator)
public static <T> Iterable<T> reverse(List<T> list)
List<String> mylist = ...
for (String str : Iterables.reverse(mylist)) {
...
}
There is no corresponding method in Iterators
, since Iterable.iterator()
can simply be invoked on the result of calling this
method.
public static <T> boolean isEmpty(Iterable<T> iterable)
There is no precise Iterator
equivalent to this method, since
one can only ask an iterator whether it has any elements remaining
(which one does using Iterator.hasNext()
).
true
if the iterable contains no elements
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |