E
- the element typepublic class FluentIterable<E>
extends java.lang.Object
implements java.lang.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 PredicateThe resulting list will contain the following elements:() { public boolean evaluate(Integer number) { return number % 2 == 0; } ) .transform(TransformerUtils.stringValueTransformer()) .limit(3) .toList();
[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(java.lang.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.
|
java.util.Enumeration<E> |
asEnumeration()
Returns an Enumeration that will enumerate all elements contained
in this iterable.
|
FluentIterable<E> |
collate(java.lang.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(java.lang.Iterable<? extends E> other,
java.util.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(java.lang.Object object)
Checks if the object is contained in this iterable.
|
void |
copyInto(java.util.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.
|
java.util.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(java.lang.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(java.lang.Class<E> arrayClass)
Returns an array containing all elements of this iterable by traversing
its iterator.
|
java.util.List<E> |
toList()
Returns a mutable list containing all elements of this iterable
by traversing its iterator.
|
java.lang.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(java.lang.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(java.lang.Iterable<? extends E> other)
Returns a new FluentIterable whose iterator will traverse
the elements of this iterable and the other iterable in
alternating order.
|
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(java.lang.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 nulljava.lang.NullPointerException
- if iterable is nullpublic FluentIterable<E> append(E... elements)
elements
- the elements to append to the iterablepublic FluentIterable<E> append(java.lang.Iterable<? extends E> other)
other
- the other iterable to combine, may not be nulljava.lang.NullPointerException
- if other is nullpublic FluentIterable<E> collate(java.lang.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 nulljava.lang.NullPointerException
- if other is nullorg.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
public FluentIterable<E> collate(java.lang.Iterable<? extends E> other, java.util.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 nulljava.lang.NullPointerException
- if other is nullorg.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
public FluentIterable<E> eval()
Calling this method is equivalent to:
FluentIterablesomeIterable = ...; FluentIterable.of(someIterable.toList());
public FluentIterable<E> filter(Predicate<? super E> predicate)
predicate
- the predicate used to filter elementsjava.lang.NullPointerException
- if predicate is nullpublic FluentIterable<E> limit(long maxSize)
maxSize
- the maximum number of elementsjava.lang.IllegalArgumentException
- if maxSize is negativepublic FluentIterable<E> loop()
public FluentIterable<E> reverse()
public FluentIterable<E> skip(long elementsToSkip)
elementsToSkip
- the number of elements to skipjava.lang.IllegalArgumentException
- if elementsToSkip is negativepublic <O> FluentIterable<O> transform(Transformer<? super E,? extends O> transformer)
O
- the output element typetransformer
- the transformer applied to each elementjava.lang.NullPointerException
- if transformer is nullpublic FluentIterable<E> unique()
public FluentIterable<E> unmodifiable()
public FluentIterable<E> zip(java.lang.Iterable<? extends E> other)
other
- the other iterable to interleave, may not be nulljava.lang.NullPointerException
- if other is nullpublic FluentIterable<E> zip(java.lang.Iterable<? extends E>... others)
others
- the iterables to interleave, may not be nulljava.lang.NullPointerException
- if either of the provided iterables is nullpublic java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
public java.util.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 nulljava.lang.NullPointerException
- 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 nulljava.lang.NullPointerException
- if predicate is nullpublic boolean isEmpty()
public boolean contains(java.lang.Object object)
object
- the object to checkpublic void forEach(Closure<? super E> closure)
closure
- the closure to apply to each element, may not be nulljava.lang.NullPointerException
- if closure is nullpublic E get(int position)
position
- the position of the element to returnjava.lang.IndexOutOfBoundsException
- if the provided position is outside the
valid range of this iterable: [0, size)public int size()
public void copyInto(java.util.Collection<? super E> collection)
collection
- the collection to add the elementsjava.lang.NullPointerException
- if collection is nullpublic E[] toArray(java.lang.Class<E> arrayClass)
arrayClass
- the class of array to createjava.lang.ArrayStoreException
- if arrayClass is invalidpublic java.util.List<E> toList()
The returned list is guaranteed to be mutable.
public java.lang.String toString()
toString
in class java.lang.Object
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"