E
- the element typepublic class FluentIterable<E> extends Object implements Iterable<E>
A FluentIterable can be created either from an Iterable or from a set of elements. The following types of methods are provided:
FluentIterable
instance,
providing a view of the original iterable (e.g. filter(Predicate));
The following example outputs the first 3 even numbers in the range [1, 10] into a list:
List<String> result = FluentIterable .of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .filter(new Predicate<Integer>() { public boolean evaluate(Integer number) { return number % 2 == 0; } ) .transform(TransformerUtils.stringValueTransformer()) .limit(3) .toList();The resulting list will contain the following elements:
[2, 4, 6]
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(Predicate<? super E> predicate)
Checks if all elements contained in this iterable are matching the
provided predicate.
|
boolean |
anyMatch(Predicate<? super E> predicate)
Checks if this iterable contains any element matching the provided predicate.
|
FluentIterable<E> |
append(E... elements)
Returns a new FluentIterable whose iterator will first traverse
the elements of the current iterable, followed by the provided
elements.
|
FluentIterable<E> |
append(Iterable<? extends E> other)
Returns a new FluentIterable whose iterator will first traverse
the elements of the current iterable, followed by the elements
of the provided iterable.
|
Enumeration<E> |
asEnumeration()
Returns an Enumeration that will enumerate all elements contained
in this iterable.
|
FluentIterable<E> |
collate(Iterable<? extends E> other)
Returns a new FluentIterable whose iterator will traverse the
elements of the current and provided iterable in natural order.
|
FluentIterable<E> |
collate(Iterable<? extends E> other,
Comparator<? super E> comparator)
Returns a new FluentIterable whose iterator will traverse the
elements of the current and provided iterable according to the
ordering defined by an comparator.
|
boolean |
contains(Object object)
Checks if the object is contained in this iterable.
|
void |
copyInto(Collection<? super E> collection)
Traverses an iterator of this iterable and adds all elements
to the provided collection.
|
static <T> FluentIterable<T> |
empty()
Creates a new empty FluentIterable.
|
FluentIterable<E> |
eval()
This method fully traverses an iterator of this iterable and returns
a new iterable with the same contents, but without any reference
to the originating iterables and/or iterators.
|
FluentIterable<E> |
filter(Predicate<? super E> predicate)
Returns a new FluentIterable whose iterator will only return
elements from this iterable matching the provided predicate.
|
void |
forEach(Closure<? super E> closure)
Applies the closure to all elements contained in this iterable.
|
E |
get(int position)
Returns the element at the provided position in this iterable.
|
boolean |
isEmpty()
Checks if this iterable is empty.
|
Iterator<E> |
iterator() |
FluentIterable<E> |
limit(long maxSize)
Returns a new FluentIterable whose iterator will return at most
the provided maximum number of elements from this iterable.
|
FluentIterable<E> |
loop()
Returns a new FluentIterable whose iterator will loop infinitely
over the elements from this iterable.
|
static <T> FluentIterable<T> |
of(Iterable<T> iterable)
Construct a new FluentIterable from the provided iterable.
|
static <T> FluentIterable<T> |
of(T... elements)
Creates a new FluentIterable from the provided elements.
|
static <T> FluentIterable<T> |
of(T singleton)
Creates a new FluentIterable of the single provided element.
|
FluentIterable<E> |
reverse()
Returns a new FluentIterable whose iterator will traverse the
elements from this iterable in reverse order.
|
int |
size()
Returns the number of elements that are contained in this iterable.
|
FluentIterable<E> |
skip(long elementsToSkip)
Returns a new FluentIterable whose iterator will skip the first
N elements from this iterable.
|
E[] |
toArray(Class<E> arrayClass)
Returns an array containing all elements of this iterable by traversing
its iterator.
|
List<E> |
toList()
Returns a mutable list containing all elements of this iterable
by traversing its iterator.
|
String |
toString() |
<O> FluentIterable<O> |
transform(Transformer<? super E,? extends O> transformer)
Returns a new FluentIterable whose iterator will return all elements
of this iterable transformed by the provided transformer.
|
FluentIterable<E> |
unique()
Returns a new FluentIterable whose iterator will return a unique view
of this iterable.
|
FluentIterable<E> |
unmodifiable()
Returns a new FluentIterable whose iterator will return an unmodifiable
view of this iterable.
|
FluentIterable<E> |
zip(Iterable<? extends E>... others)
Returns a new FluentIterable whose iterator will traverse
the elements of this iterable and the other iterables in
alternating order.
|
FluentIterable<E> |
zip(Iterable<? extends E> other)
Returns a new FluentIterable whose iterator will traverse
the elements of this iterable and the other iterable in
alternating order.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static <T> FluentIterable<T> empty()
T
- the element typepublic static <T> FluentIterable<T> of(T singleton)
The returned iterable's iterator does not support remove()
.
T
- the element typesingleton
- the singleton elementpublic static <T> FluentIterable<T> of(T... elements)
The returned iterable's iterator does not support remove()
.
T
- the element typeelements
- the elements to be contained in the FluentIterablepublic static <T> FluentIterable<T> of(Iterable<T> iterable)
The returned iterable's iterator supports remove()
when the
corresponding input iterator supports it.
T
- the element typeiterable
- the iterable to wrap into a FluentIterable, may not be nullNullPointerException
- if iterable is nullpublic FluentIterable<E> append(E... elements)
elements
- the elements to append to the iterablepublic FluentIterable<E> append(Iterable<? extends E> other)
other
- the other iterable to combine, may not be nullNullPointerException
- if other is nullpublic FluentIterable<E> collate(Iterable<? extends E> other)
Example: natural ordering
The returned iterable will traverse the elements in the following order: [1, 2, 3, 4, 5, 6, 7, 8]
other
- the other iterable to collate, may not be nullNullPointerException
- if other is nullCollatingIterator
public FluentIterable<E> collate(Iterable<? extends E> other, Comparator<? super E> comparator)
Example: descending order
The returned iterable will traverse the elements in the following order: [8, 7, 6, 5, 4, 3, 2, 1]
comparator
- the comparator to define an ordering, may be null,
in which case natural ordering will be usedother
- the other iterable to collate, may not be nullNullPointerException
- if other is nullCollatingIterator
public FluentIterable<E> eval()
Calling this method is equivalent to:
FluentIterable<E> someIterable = ...; FluentIterable.of(someIterable.toList());
public FluentIterable<E> filter(Predicate<? super E> predicate)
predicate
- the predicate used to filter elementsNullPointerException
- if predicate is nullpublic FluentIterable<E> limit(long maxSize)
maxSize
- the maximum number of elementsIllegalArgumentException
- if maxSize is negativepublic FluentIterable<E> loop()
public FluentIterable<E> reverse()
public FluentIterable<E> skip(long elementsToSkip)
elementsToSkip
- the number of elements to skipIllegalArgumentException
- if elementsToSkip is negativepublic <O> FluentIterable<O> transform(Transformer<? super E,? extends O> transformer)
O
- the output element typetransformer
- the transformer applied to each elementNullPointerException
- if transformer is nullpublic FluentIterable<E> unique()
public FluentIterable<E> unmodifiable()
public FluentIterable<E> zip(Iterable<? extends E> other)
other
- the other iterable to interleave, may not be nullNullPointerException
- if other is nullpublic FluentIterable<E> zip(Iterable<? extends E>... others)
others
- the iterables to interleave, may not be nullNullPointerException
- if either of the provided iterables is nullpublic Enumeration<E> asEnumeration()
public boolean allMatch(Predicate<? super E> predicate)
A null
or empty iterable returns true.
predicate
- the predicate to use, may not be nullNullPointerException
- if predicate is nullpublic boolean anyMatch(Predicate<? super E> predicate)
A null
or empty iterable returns false.
predicate
- the predicate to use, may not be nullNullPointerException
- if predicate is nullpublic boolean isEmpty()
public boolean contains(Object object)
object
- the object to checkpublic void forEach(Closure<? super E> closure)
closure
- the closure to apply to each element, may not be nullNullPointerException
- if closure is nullpublic E get(int position)
position
- the position of the element to returnIndexOutOfBoundsException
- if the provided position is outside the
valid range of this iterable: [0, size)public int size()
public void copyInto(Collection<? super E> collection)
collection
- the collection to add the elementsNullPointerException
- if collection is nullpublic E[] toArray(Class<E> arrayClass)
arrayClass
- the class of array to createArrayStoreException
- if arrayClass is invalidpublic List<E> toList()
The returned list is guaranteed to be mutable.
Copyright © 2001–2019 The Apache Software Foundation. All rights reserved.