|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.collect.FluentIterable<E>
@Beta @GwtCompatible(emulated=true) public abstract class FluentIterable<E>
FluentIterable
provides a rich interface for manipulating Iterable
s in a chained
fashion. A FluentIterable
can be created from an Iterable
, or from a set of
elements. The following types of methods are provided on FluentIterable
:
FluentIterable
based in some way on the contents
of the current one (for example transform(com.google.common.base.Function super E, T>)
)
FluentIterable
's contents into a new collection or
array (for example toImmutableList()
)
last()
)
FluentIterable
's contents (for example
anyMatch(com.google.common.base.Predicate super E>)
)
Here is an example that merges the lists returned by two separate database calls, transforms
it by invoking toString()
on each element, and returns the first 10 elements as an
ImmutableList
:
FluentIterable
.from(database.getClientList())
.transform(Functions.toStringFunction())
.limit(10)
.toImmutableList();
Anything which can be done using FluentIterable
could be done in a different fashion
(often with Iterables
), however the use of FluentIterable
makes many sets of
operations significantly more concise.
Constructor Summary | |
---|---|
protected |
FluentIterable()
Constructor for use by subclasses. |
Method Summary | ||
---|---|---|
boolean |
allMatch(Predicate<? super E> predicate)
Returns true if every element in this fluent iterable satisfies the predicate. |
|
boolean |
anyMatch(Predicate<? super E> predicate)
Returns true if any element in this fluent iterable satisfies the predicate. |
|
boolean |
contains(Object element)
Returns true if this fluent iterable contains any object for which
equals(element) is true. |
|
FluentIterable<E> |
cycle()
Returns a fluent iterable whose Iterator cycles indefinitely over the elements of
this fluent iterable. |
|
|
filter(Class<T> type)
Returns the elements from this fluent iterable that are instances of class type . |
|
FluentIterable<E> |
filter(Predicate<? super E> predicate)
Returns the elements from this fluent iterable that satisfy a predicate. |
|
Optional<E> |
first()
Returns an Optional containing the first element in this fluent iterable. |
|
Optional<E> |
firstMatch(Predicate<? super E> predicate)
Returns an Optional containing the first element in this fluent iterable that
satisfies the given predicate, if such an element exists. |
|
static
|
from(FluentIterable<E> iterable)
Deprecated. instances of FluentIterable don't need to be converted to
FluentIterable |
|
static
|
from(Iterable<E> iterable)
Returns a fluent iterable that wraps iterable , or iterable itself if it
is already a FluentIterable . |
|
E |
get(int position)
Returns the element at the specified position in this fluent iterable. |
|
boolean |
isEmpty()
Determines whether this fluent iterable is empty. |
|
Optional<E> |
last()
Returns an Optional containing the last element in this fluent iterable. |
|
FluentIterable<E> |
limit(int size)
Creates a fluent iterable with the first size elements of this
fluent iterable. |
|
int |
size()
Returns the number of elements in this fluent iterable. |
|
FluentIterable<E> |
skip(int numberToSkip)
Returns a view of this fluent iterable that skips its first numberToSkip
elements. |
|
E[] |
toArray(Class<E> type)
Returns an array containing all of the elements from this fluent iterable in iteration order. |
|
ImmutableList<E> |
toImmutableList()
Returns an ImmutableList containing all of the elements from this
fluent iterable in proper sequence. |
|
ImmutableSet<E> |
toImmutableSet()
Returns an ImmutableSet containing all of the elements from this
fluent iterable with duplicates removed. |
|
ImmutableSortedSet<E> |
toImmutableSortedSet(Comparator<? super E> comparator)
Returns an ImmutableSortedSet containing all of the elements from this
FluentIterable in the order specified by comparator , with duplicates
(determined by comaprator.compare(x, y) == 0 ) removed. |
|
String |
toString()
Returns a string representation of this fluent iterable, with the format [e1, e2, ..., en] . |
|
|
transform(Function<? super E,T> function)
Returns a fluent iterable that applies function to each element of this
fluent iterable. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.lang.Iterable |
---|
iterator |
Constructor Detail |
---|
protected FluentIterable()
Method Detail |
---|
public static <E> FluentIterable<E> from(Iterable<E> iterable)
iterable
, or iterable
itself if it
is already a FluentIterable
.
@Deprecated public static <E> FluentIterable<E> from(FluentIterable<E> iterable)
FluentIterable
don't need to be converted to
FluentIterable
Iterable
to
FluentIterable
has obviated the need to explicitly convert to a FluentIterable
.
public String toString()
[e1, e2, ..., en]
.
toString
in class Object
public final int size()
public final boolean contains(@Nullable Object element)
true
if this fluent iterable contains any object for which
equals(element)
is true.
public final FluentIterable<E> cycle()
Iterator
cycles indefinitely over the elements of
this fluent iterable.
That iterator supports remove()
if iterable.iterator()
does. After
remove()
is called, subsequent cycles omit the removed element, which is no longer in
this fluent iterable. The iterator's hasNext()
method returns true
until
this fluent 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.
public final FluentIterable<E> filter(Predicate<? super E> predicate)
remove()
.
@GwtIncompatible(value="Class.isInstance") public final <T> FluentIterable<T> filter(Class<T> type)
type
.
type
- the type of elements desiredpublic final boolean anyMatch(Predicate<? super E> predicate)
true
if any element in this fluent iterable satisfies the predicate.
public final boolean allMatch(Predicate<? super E> predicate)
true
if every element in this fluent iterable satisfies the predicate.
If this fluent iterable is empty, true
is returned.
public final Optional<E> firstMatch(Predicate<? super E> predicate)
Optional
containing the first element in this fluent iterable that
satisfies the given predicate, if such an element exists.
Warning: avoid using a predicate
that matches null
. If null
is matched in this fluent iterable, a NullPointerException
will be thrown.
public final <T> FluentIterable<T> transform(Function<? super E,T> function)
function
to each element of this
fluent iterable.
The returned fluent iterable's iterator supports remove()
if this iterable's
iterator does. After a successful remove()
call, this fluent iterable no longer
contains the corresponding element.
public final Optional<E> first()
Optional
containing the first element in this fluent iterable.
If the iterable is empty, Optional.absent()
is returned.
NullPointerException
- if the first element is null; if this is a possibility, use
iterator().next()
or Iterables.getFirst(java.lang.Iterable extends T>, T)
instead.public final Optional<E> last()
Optional
containing the last element in this fluent iterable.
If the iterable is empty, Optional.absent()
is returned.
NullPointerException
- if the last element is null; if this is a possibility, use
Iterables.getLast(java.lang.Iterable)
instead.public final FluentIterable<E> skip(int numberToSkip)
numberToSkip
elements. If this fluent iterable contains fewer than numberToSkip
elements,
the returned fluent iterable skips all of its elements.
Modifications to this fluent iterable before a call to iterator()
are
reflected in the returned fluent iterable. That is, the its iterator skips the first
numberToSkip
elements that exist when the iterator is created, not when skip()
is called.
The returned fluent iterable's iterator supports remove()
if the
Iterator
of this fluent iterable supports it. Note that it is not
possible to delete the last skipped element by immediately calling remove()
on the
returned fluent iterable's iterator, as the Iterator
contract states that a call
to * remove()
before a call to next()
will throw an
IllegalStateException
.
public final FluentIterable<E> limit(int size)
size
elements of this
fluent iterable. If this fluent iterable does not contain that many elements,
the returned fluent iterable will have the same behavior as this fluent iterable.
The returned fluent iterable's iterator supports remove()
if this
fluent iterable's iterator does.
size
- the maximum number of elements in the returned fluent iterable
IllegalArgumentException
- if size
is negativepublic final boolean isEmpty()
public final ImmutableList<E> toImmutableList()
ImmutableList
containing all of the elements from this
fluent iterable in proper sequence.
public final ImmutableSet<E> toImmutableSet()
ImmutableSet
containing all of the elements from this
fluent iterable with duplicates removed.
public final ImmutableSortedSet<E> toImmutableSortedSet(Comparator<? super E> comparator)
ImmutableSortedSet
containing all of the elements from this
FluentIterable
in the order specified by comparator
, with duplicates
(determined by comaprator.compare(x, y) == 0
) removed. To produce an
ImmutableSortedSet
sorted by its natural ordering, use
toImmutableSortedSet(Ordering.natural())
.
comparator
- the function by which to sort set elements
NullPointerException
- if any element is null@GwtIncompatible(value="Array.newArray(Class, int)") public final E[] toArray(Class<E> type)
type
- the type of the elements
public final E get(int position)
position
- position of the element to return
IndexOutOfBoundsException
- if position
is negative or greater than or equal to
the size of this fluent iterable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |