|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.Iterables
public final class Iterables
This class contains static utility methods that operate on or return objects
of type Iterable. Also see the parallel implementations in Iterators.
| Method Summary | ||
|---|---|---|
static
|
addAll(java.util.Collection<T> collection,
java.lang.Iterable<? extends T> iterable)
Adds all elements in iterable to collection. |
|
static
|
all(java.lang.Iterable<T> iterable,
Predicate<? super T> predicate)
Returns true if no element in iterable evaluates to false under predicate. |
|
static
|
any(java.lang.Iterable<T> iterable,
Predicate<? super T> predicate)
Returns true if some element in iterable evaluates to
true under predicate. |
|
static
|
concat(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> iterables)
Variant of Iterators.concat that acts on and returns instances of
Iterable. |
|
static
|
concat(java.lang.Iterable<? extends T>... iterables)
Variant of Iterators.concat that acts on and returns instances of
Iterable. |
|
static
|
concat(java.lang.Iterable<? extends T> firstElements,
java.lang.Iterable<? extends T> nextElements)
Variant of Iterators.concat that acts on and returns instances of
Iterable. |
|
static
|
cycle(java.lang.Iterable<T> iterable)
Variant of Iterators.cycle which returns an Iterable. |
|
static
|
cycle(T... elements)
Variant of cycle(Iterable) accepting varargs parameters. |
|
static boolean |
elementsEqual(java.lang.Iterable<?> iterable1,
java.lang.Iterable<?> iterable2)
Determines whether the two Iterables contain equal elements. |
|
static
|
emptyIterable()
Returns the empty Iterable. |
|
static
|
filter(java.lang.Iterable<?> unfiltered,
java.lang.Class<T> type)
Returns all instances of type found in unfiltered. |
|
static
|
filter(java.lang.Iterable<T> unfiltered,
Predicate<? super T> predicate)
Variant of Iterators.filter(Iterator,Predicate), which accepts and
returns an iterable instead of an iterator. |
|
static
|
find(java.lang.Iterable<E> iterable,
Predicate<? super E> predicate)
Returns the first element in iterable for which the given predicate
matches. |
|
static int |
frequency(java.lang.Iterable<?> iterable,
java.lang.Object element)
Variant of Collections.frequency for iterables. |
|
static
|
getLast(java.lang.Iterable<T> iterable)
Returns the last element of iterable, or throws a
NoSuchElementException if it has no elements. |
|
static
|
getOnlyElement(java.lang.Iterable<T> iterable)
Returns the single element contained in iterable. |
|
static
|
getOnlyElement(java.lang.Iterable<T> iterable,
T defaultValue)
Returns the single element contained in iterator, or defaultValue if the iterator is empty. |
|
static
|
isEmpty(java.lang.Iterable<T> iterable)
Returns whether the given iterable contains no elements. |
|
static
|
limit(java.lang.Iterable<T> iterable,
int limit)
Variant of Iterators.limit(Iterator,int), which accepts and
returns an iterable instead of an iterator. |
|
static
|
newArray(java.lang.Iterable<T> iterable,
java.lang.Class<T> type)
Converts an Iterable into an array. |
|
static
|
partition(java.lang.Iterable<? extends T> iterable,
int partitionSize,
boolean padToSize)
Partition an iterable into sub iterables of the given size like so: {A, B, C, D, E, F} with partition size 3 => {A, B, C} and {D, E, F}. |
|
static
|
reverse(java.util.List<T> list)
Adapt a list to an Iterable over a reversed version of the list. |
|
static
|
rotate(java.util.List<T> list,
int distance)
Provides a rotated view of a list. |
|
static int |
size(java.lang.Iterable<?> iterable)
Returns the number of elements present in iterable. |
|
static
|
skip(java.lang.Iterable<T> iterable,
int numberToSkip)
Returns a view of iterable whose Iterator skips its first
numberToSkip elements or, if iterable contains fewer than
numberToSkip elements, skips all its elements. |
|
static java.lang.String |
toString(java.lang.Iterable<?> iterable)
Returns a string representation of iterable in the same format as
Iterators#toString(java.util.Iterator). |
|
static
|
transform(java.lang.Iterable<F> fromIterable,
Function<? super F,? extends T> function)
Returns an iterable that applies function to each element of fromIterable. |
|
static
|
unmodifiableIterable(java.lang.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> java.lang.Iterable<T> emptyIterable()
public static <T> java.lang.Iterable<T> unmodifiableIterable(java.lang.Iterable<T> iterable)
iterable.
public static int size(java.lang.Iterable<?> iterable)
iterable.
public static boolean elementsEqual(java.lang.Iterable<?> iterable1,
java.lang.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 java.lang.String toString(java.lang.Iterable<?> iterable)
iterable in the same format as
Iterators#toString(java.util.Iterator).
public static <T> T getOnlyElement(java.lang.Iterable<T> iterable)
iterable.
java.util.NoSuchElementException - if the iterable is empty
java.lang.IllegalArgumentException - if the iterable contains multiple
elements
public static <T> T getOnlyElement(java.lang.Iterable<T> iterable,
@Nullable
T defaultValue)
iterator, or defaultValue if the iterator is empty.
java.lang.IllegalArgumentException - if the iterator contains multiple
elements
public static <T> T[] newArray(java.lang.Iterable<T> iterable,
java.lang.Class<T> type)
Iterable into an array.
iterable - any instance of Iterable (will not be modified)type - the type of the elements
public static <T> boolean addAll(java.util.Collection<T> collection,
java.lang.Iterable<? extends T> iterable)
iterable to collection.
true if collection was modified as a result of this
operation.
public static int frequency(java.lang.Iterable<?> iterable,
@Nullable
java.lang.Object element)
Collections.frequency for iterables.
public static <T> java.lang.Iterable<T> cycle(java.lang.Iterable<T> iterable)
Iterators.cycle which returns an Iterable.
Iterators.cycle(Iterable)public static <T> java.lang.Iterable<T> cycle(T... elements)
cycle(Iterable) accepting varargs parameters.
public static <T> java.lang.Iterable<T> concat(java.lang.Iterable<? extends T> firstElements,
java.lang.Iterable<? extends T> nextElements)
Iterators.concat that acts on and returns instances of
Iterable.
public static <T> java.lang.Iterable<T> concat(java.lang.Iterable<? extends T>... iterables)
Iterators.concat that acts on and returns instances of
Iterable.
public static <T> java.lang.Iterable<T> concat(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> iterables)
Iterators.concat that acts on and returns instances of
Iterable.
public static <T> java.lang.Iterable<java.lang.Iterable<T>> partition(java.lang.Iterable<? extends T> iterable,
int partitionSize,
boolean padToSize)
NOTE: You must read partitions one at a time from the returned iterable Once you read forward any iterables from previous partitions will become invalid.
NOTE: This is optimized for a the simple case of iterating through each sub-iterable in order only once. Other operations will succeed, but will suffer a performance penalty to maintain correctness.
iterable - the iterable to partitionpartitionSize - the size of each partitionpadToSize - whether to pad the last partition to the partition size
with null.
public static <T> java.lang.Iterable<T> filter(java.lang.Iterable<T> unfiltered,
Predicate<? super T> predicate)
Iterators.filter(Iterator,Predicate), which accepts and
returns an iterable instead of an iterator.
Iterators.filter(Iterator, Predicate)
public static <T> java.lang.Iterable<T> filter(java.lang.Iterable<?> unfiltered,
java.lang.Class<T> type)
type found in unfiltered. Similar
to filter(Iterable, Predicate).
unfiltered - an iterable containing objects of any typetype - the type of elements desired
public static <T> boolean any(java.lang.Iterable<T> iterable,
Predicate<? super T> predicate)
true if some element in iterable evaluates to
true under predicate. Returns false if iterable is empty.
public static <T> boolean all(java.lang.Iterable<T> iterable,
Predicate<? super T> predicate)
true if no element in iterable evaluates to false under predicate. Returns true if iterable is
empty.
public static <E> E find(java.lang.Iterable<E> iterable,
Predicate<? super E> predicate)
iterable for which the given predicate
matches.
java.util.NoSuchElementException - if no element in iterable matches
the given predicate
public static <F,T> java.lang.Iterable<T> transform(java.lang.Iterable<F> fromIterable,
Function<? super F,? extends T> function)
function to each element of fromIterable.
public static <T> java.lang.Iterable<T> reverse(java.util.List<T> list)
Listmylist = ... for (String str : Iterables.reverse(mylist)) { ... }
public static <T> java.lang.Iterable<T> rotate(java.util.List<T> list,
int distance)
Collections.rotate(java.util.List>, int)
only in that it leaves the underlying list unchanged. Note that this is a
"live" view of the list that will change as the list changes. However, the
behavior of an Iterator constructed from a rotated view of the list
is undefined if the list is changed after the Iterator is constructed.
list - the list to return a rotated view of.distance - the distance to rotate the list. There are no constraints
on this value; it may be zero, negative, or greater than list.size().
public static <T> java.lang.Iterable<T> limit(java.lang.Iterable<T> iterable,
int limit)
Iterators.limit(Iterator,int), which accepts and
returns an iterable instead of an iterator.
Iterators.limit(Iterator, int)public static <T> boolean isEmpty(java.lang.Iterable<T> iterable)
true if the iterable has no elements, false if the
iterable has one or more elements
public static <T> java.lang.Iterable<T> skip(java.lang.Iterable<T> iterable,
int numberToSkip)
iterable whose Iterator skips its first
numberToSkip elements or, if iterable contains fewer than
numberToSkip elements, skips all its elements.
Structural modifications to the underlying Iterable
before a call to iterator()
are reflected in the returned Iterator. That is, the returned
Iterator skips the first numberToSkip elements that exist
when the Iterator is created, not when skip() is called.
Structural modifications to the underlying Iterable
after a call to iterator() may result in undefined
behavior by the returned Iterator, depending upon the
concurrent-modification policy of the underlying Iterable.
Non-structural changes to the underlying Iterable are always
reflected in the returned Iterable.
Iterator.remove() is supported if iterators of the underlying
Iterable supports it. (Note that it is not
possible to delete the last skipped element by immediately calling
remove() on a new iterator, as the Iterator contract
requires that a call to remove() before a call to
next() will throw an
IllegalStateException.)
public static <T> T getLast(java.lang.Iterable<T> iterable)
iterable, or throws a
NoSuchElementException if it has no elements.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||