Class FluentIterable<E>
- All Implemented Interfaces:
Iterable<E>
FluentIterable provides a rich interface for manipulating Iterable instances 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:
- chained methods which return a new
FluentIterablebased in some way on the contents of the current one (for exampletransform(com.google.common.base.Function<? super E, T>)) - conversion methods which copy the
FluentIterable's contents into a new collection or array (for exampletoList()) - element extraction methods which facilitate the retrieval of certain elements (for example
last()) - query methods which answer questions about the
FluentIterable's contents (for exampleanyMatch(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())
.filter(activeInLastMonth())
.transform(Functions.toStringFunction())
.limit(10)
.toList();
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.
- Since:
- 12.0
-
Method Summary
Modifier and TypeMethodDescriptionfinal booleanDeprecated.Returnstrueif every element in this fluent iterable satisfies the predicate.final booleanDeprecated.Returnstrueif any element in this fluent iterable satisfies the predicate.final booleanDeprecated.Returnstrueif this fluent iterable contains any object for whichequals(element)is true.final <C extends Collection<? super E>>
CcopyInto(C collection) Deprecated.Copies all the elements from this fluent iterable tocollection.final FluentIterable<E> cycle()Deprecated.Returns a fluent iterable whoseIteratorcycles indefinitely over the elements of this fluent iterable.final FluentIterable<E> Deprecated.Returns the elements from this fluent iterable that satisfy a predicate.final <T> FluentIterable<T> Deprecated.Returns the elements from this fluent iterable that are instances of classtype.first()Deprecated.Returns anOptionalcontaining the first element in this fluent iterable.firstMatch(Predicate<? super E> predicate) Deprecated.Returns anOptionalcontaining the first element in this fluent iterable that satisfies the given predicate, if such an element exists.static <E> FluentIterable<E> from(FluentIterable<E> iterable) Deprecated.instances ofFluentIterabledon't need to be converted toFluentIterablestatic <E> FluentIterable<E> Deprecated.Returns a fluent iterable that wrapsiterable, oriterableitself if it is already aFluentIterable.final Eget(int position) Deprecated.Returns the element at the specified position in this fluent iterable.final <K> ImmutableListMultimap<K, E> Deprecated.Creates an indexImmutableListMultimapthat contains the results of applying a specified function to each item in thisFluentIterableof values.final booleanisEmpty()Deprecated.Determines whether this fluent iterable is empty.last()Deprecated.Returns anOptionalcontaining the last element in this fluent iterable.final FluentIterable<E> limit(int size) Deprecated.Creates a fluent iterable with the firstsizeelements of this fluent iterable.final intsize()Deprecated.Returns the number of elements in this fluent iterable.final FluentIterable<E> skip(int numberToSkip) Deprecated.Returns a view of this fluent iterable that skips its firstnumberToSkipelements.final E[]Deprecated.Returns an array containing all of the elements from this fluent iterable in iteration order.final ImmutableList<E> toList()Deprecated.Returns anImmutableListcontaining all of the elements from this fluent iterable in proper sequence.final <V> ImmutableMap<E, V> Deprecated.Returns an immutable map for which the elements of thisFluentIterableare the keys in the same order, mapped to values by the given function.final ImmutableSet<E> toSet()Deprecated.Returns anImmutableSetcontaining all of the elements from this fluent iterable with duplicates removed.final ImmutableList<E> toSortedList(Comparator<? super E> comparator) Deprecated.Returns anImmutableListcontaining all of the elements from thisFluentIterablein the order specified bycomparator.final ImmutableSortedSet<E> toSortedSet(Comparator<? super E> comparator) Deprecated.Returns anImmutableSortedSetcontaining all of the elements from thisFluentIterablein the order specified bycomparator, with duplicates (determined bycomparator.compare(x, y) == 0) removed.toString()Deprecated.Returns a string representation of this fluent iterable, with the format[e1, e2, ..., en].final <T> FluentIterable<T> Deprecated.Returns a fluent iterable that appliesfunctionto each element of this fluent iterable.<T> FluentIterable<T> transformAndConcat(Function<? super E, ? extends Iterable<? extends T>> function) Deprecated.Appliesfunctionto each element of this fluent iterable and returns a fluent iterable with the concatenated combination of results.final <K> ImmutableMap<K, E> uniqueIndex(Function<? super E, K> keyFunction) Deprecated.Returns an immutable map for which theMap.values()are the elements of thisFluentIterablein the given order, and each key is the product of invoking a supplied function on its corresponding value.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
from
Deprecated.Returns a fluent iterable that wrapsiterable, oriterableitself if it is already aFluentIterable. -
from
Deprecated.instances ofFluentIterabledon't need to be converted toFluentIterableConstruct a fluent iterable from another fluent iterable. This is obviously never necessary, but is intended to help call out cases where one migration fromIterabletoFluentIterablehas obviated the need to explicitly convert to aFluentIterable. -
toString
Deprecated.Returns a string representation of this fluent iterable, with the format[e1, e2, ..., en]. -
size
public final int size()Deprecated.Returns the number of elements in this fluent iterable. -
contains
Deprecated.Returnstrueif this fluent iterable contains any object for whichequals(element)is true. -
cycle
Deprecated.Returns a fluent iterable whoseIteratorcycles indefinitely over the elements of this fluent iterable.That iterator supports
remove()ifiterable.iterator()does. Afterremove()is called, subsequent cycles omit the removed element, which is no longer in this fluent iterable. The iterator'shasNext()method returnstrueuntil this fluent iterable is empty.Warning: Typical uses of the resulting iterator may produce an infinite loop. You should use an explicit
breakor be certain that you will eventually remove all the elements. -
filter
Deprecated.Returns the elements from this fluent iterable that satisfy a predicate. The resulting fluent iterable's iterator does not supportremove(). -
filter
@GwtIncompatible("Class.isInstance") @CheckReturnValue public final <T> FluentIterable<T> filter(Class<T> type) Deprecated.Returns the elements from this fluent iterable that are instances of classtype.- Parameters:
type- the type of elements desired
-
anyMatch
Deprecated.Returnstrueif any element in this fluent iterable satisfies the predicate. -
allMatch
Deprecated.Returnstrueif every element in this fluent iterable satisfies the predicate. If this fluent iterable is empty,trueis returned. -
firstMatch
Deprecated.Returns anOptionalcontaining the first element in this fluent iterable that satisfies the given predicate, if such an element exists.Warning: avoid using a
predicatethat matchesnull. Ifnullis matched in this fluent iterable, aNullPointerExceptionwill be thrown. -
transform
Deprecated.Returns a fluent iterable that appliesfunctionto each element of this fluent iterable.The returned fluent iterable's iterator supports
remove()if this iterable's iterator does. After a successfulremove()call, this fluent iterable no longer contains the corresponding element. -
transformAndConcat
public <T> FluentIterable<T> transformAndConcat(Function<? super E, ? extends Iterable<? extends T>> function) Deprecated.Appliesfunctionto each element of this fluent iterable and returns a fluent iterable with the concatenated combination of results.functionreturns an Iterable of results.The returned fluent iterable's iterator supports
remove()if this function-returned iterables' iterator does. After a successfulremove()call, the returned fluent iterable no longer contains the corresponding element.- Since:
- 13.0 (required
Function<E, Iterable<T>>until 14.0)
-
first
Deprecated.Returns anOptionalcontaining the first element in this fluent iterable. If the iterable is empty,Optional.absent()is returned.- Throws:
NullPointerException- if the first element is null; if this is a possibility, useiterator().next()orIterables.getFirst(java.lang.Iterable<? extends T>, T)instead.
-
last
Deprecated.Returns anOptionalcontaining the last element in this fluent iterable. If the iterable is empty,Optional.absent()is returned.- Throws:
NullPointerException- if the last element is null; if this is a possibility, useIterables.getLast(java.lang.Iterable<T>)instead.
-
skip
Deprecated.Returns a view of this fluent iterable that skips its firstnumberToSkipelements. If this fluent iterable contains fewer thannumberToSkipelements, 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 firstnumberToSkipelements that exist when the iterator is created, not whenskip()is called.The returned fluent iterable's iterator supports
remove()if theIteratorof this fluent iterable supports it. Note that it is not possible to delete the last skipped element by immediately callingremove()on the returned fluent iterable's iterator, as theIteratorcontract states that a call to* remove()before a call tonext()will throw anIllegalStateException. -
limit
Deprecated.Creates a fluent iterable with the firstsizeelements 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 supportsremove()if this fluent iterable's iterator does.- Parameters:
size- the maximum number of elements in the returned fluent iterable- Throws:
IllegalArgumentException- ifsizeis negative
-
isEmpty
public final boolean isEmpty()Deprecated.Determines whether this fluent iterable is empty. -
toList
Deprecated.Returns anImmutableListcontaining all of the elements from this fluent iterable in proper sequence.- Since:
- 14.0 (since 12.0 as
toImmutableList()).
-
toSortedList
Deprecated.Returns anImmutableListcontaining all of the elements from thisFluentIterablein the order specified bycomparator. To produce anImmutableListsorted by its natural ordering, usetoSortedList(Ordering.natural()).- Parameters:
comparator- the function by which to sort list elements- Throws:
NullPointerException- if any element is null- Since:
- 14.0 (since 13.0 as
toSortedImmutableList()).
-
toSet
Deprecated.Returns anImmutableSetcontaining all of the elements from this fluent iterable with duplicates removed.- Since:
- 14.0 (since 12.0 as
toImmutableSet()).
-
toSortedSet
Deprecated.Returns anImmutableSortedSetcontaining all of the elements from thisFluentIterablein the order specified bycomparator, with duplicates (determined bycomparator.compare(x, y) == 0) removed. To produce anImmutableSortedSetsorted by its natural ordering, usetoSortedSet(Ordering.natural()).- Parameters:
comparator- the function by which to sort set elements- Throws:
NullPointerException- if any element is null- Since:
- 14.0 (since 12.0 as
toImmutableSortedSet()).
-
toMap
Deprecated.Returns an immutable map for which the elements of thisFluentIterableare the keys in the same order, mapped to values by the given function. If this iterable contains duplicate elements, the returned map will contain each distinct element once in the order it first appears.- Throws:
NullPointerException- if any element of this iterable isnull, or ifvalueFunctionproducesnullfor any key- Since:
- 14.0
-
index
Deprecated.Creates an indexImmutableListMultimapthat contains the results of applying a specified function to each item in thisFluentIterableof values. Each element of this iterable will be stored as a value in the resulting multimap, yielding a multimap with the same size as this iterable. The key used to store that value in the multimap will be the result of calling the function on that value. The resulting multimap is created as an immutable snapshot. In the returned multimap, keys appear in the order they are first encountered, and the values corresponding to each key appear in the same order as they are encountered.- Parameters:
keyFunction- the function used to produce the key for each value- Throws:
NullPointerException- if any of the following cases is true:keyFunctionis null- An element in this fluent iterable is null
keyFunctionreturnsnullfor any element of this iterable
- Since:
- 14.0
-
uniqueIndex
Deprecated.Returns an immutable map for which theMap.values()are the elements of thisFluentIterablein the given order, and each key is the product of invoking a supplied function on its corresponding value.- Parameters:
keyFunction- the function used to produce the key for each value- Throws:
IllegalArgumentException- ifkeyFunctionproduces the same key for more than one value in this fluent iterableNullPointerException- if any element of this fluent iterable is null, or ifkeyFunctionproducesnullfor any value- Since:
- 14.0
-
toArray
Deprecated.Returns an array containing all of the elements from this fluent iterable in iteration order.- Parameters:
type- the type of the elements- Returns:
- a newly-allocated array into which all the elements of this fluent iterable have been copied
-
copyInto
Deprecated.Copies all the elements from this fluent iterable tocollection. This is equivalent to callingIterables.addAll(collection, this).- Parameters:
collection- the collection to copy elements to- Returns:
collection, for convenience- Since:
- 14.0
-
get
Deprecated.Returns the element at the specified position in this fluent iterable.- Parameters:
position- position of the element to return- Returns:
- the element at the specified position in this fluent iterable
- Throws:
IndexOutOfBoundsException- ifpositionis negative or greater than or equal to the size of this fluent iterable
-