E - the element type
public class FluentIterable<E> extends Objectimplements 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
() {
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
Checks if all elements contained in this iterable are matching the provided predicate.
|
boolean |
anyMatch(Predicate
Checks if this iterable contains any element matching the provided predicate.
|
FluentIterable |
append(E... elements)
Returns a new FluentIterable whose iterator will first traverse the elements of the current iterable, followed by the provided elements.
|
FluentIterable |
append(Iterable
Returns a new FluentIterable whose iterator will first traverse the elements of the current iterable, followed by the elements of the provided iterable.
|
Enumeration |
asEnumeration()
Returns an Enumeration that will enumerate all elements contained in this iterable.
|
FluentIterable |
collate(Iterable
Returns a new FluentIterable whose iterator will traverse the elements of the current and provided iterable in natural order.
|
FluentIterable |
collate(Iterable
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
Checks if the object is contained in this iterable.
|
void |
copyInto(Collection
Traverses an iterator of this iterable and adds all elements to the provided collection.
|
static <T> FluentIterable |
empty()
Creates a new empty FluentIterable.
|
FluentIterable |
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 |
filter(Predicate
Returns a new FluentIterable whose iterator will only return elements from this iterable matching the provided predicate.
|
void |
forEach(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 |
iterator()
|
FluentIterable |
limit(long maxSize)
Returns a new FluentIterable whose iterator will return at most the provided maximum number of elements from this iterable.
|
FluentIterable |
loop()
Returns a new FluentIterable whose iterator will loop infinitely over the elements from this iterable.
|
static <T> FluentIterable |
of(Iterable
Construct a new FluentIterable from the provided iterable.
|
static <T> FluentIterable |
of(T... elements)
Creates a new FluentIterable from the provided elements.
|
static <T> FluentIterable |
of(T singleton)
Creates a new FluentIterable of the single provided element.
|
FluentIterable |
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 |
skip(long elementsToSkip)
Returns a new FluentIterable whose iterator will skip the first N elements from this iterable.
|
E[] |
toArray(Class
Returns an array containing all elements of this iterable by traversing its iterator.
|
List |
toList()
Returns a mutable list containing all elements of this iterable by traversing its iterator.
|
String |
toString()
|
<O> FluentIterable |
transform(Transformer
Returns a new FluentIterable whose iterator will return all elements of this iterable transformed by the provided transformer.
|
FluentIterable |
unique()
Returns a new FluentIterable whose iterator will return a unique view of this iterable.
|
FluentIterable |
unmodifiable()
Returns a new FluentIterable whose iterator will return an unmodifiable view of this iterable.
|
FluentIterable |
zip(Iterable
Returns a new FluentIterable whose iterator will traverse the elements of this iterable and the other iterables in alternating order.
|
FluentIterable |
zip(Iterable
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, waitforEach, spliteratorpublic static <T> FluentIterable<T> empty()
T - the element type
public static <T> FluentIterable<T> of(T singleton)
The returned iterable's iterator does not support remove().
T - the element type
singleton - the singleton element
public static <T> FluentIterable<T> of(T... elements)
The returned iterable's iterator does not support remove().
T - the element type
elements - the elements to be contained in the FluentIterable
public 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 type
iterable - the iterable to wrap into a FluentIterable, may not be null
NullPointerException - if iterable is null
public FluentIterable<E> append(E... elements)
elements - the elements to append to the iterable
public FluentIterable<E> append(Iterable <? extends E> other)
other - the other iterable to combine, may not be null
NullPointerException - if other is null
public 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 null
NullPointerException - if other is null
org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
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 used
other - the other iterable to collate, may not be null
NullPointerException - if other is null
org.apache.commons.collections4.iterators.CollatingIterator CollatingIterator}
public FluentIterable<E> eval()
Calling this method is equivalent to:
FluentIterable
someIterable = ...;
FluentIterable.of(someIterable.toList());
public FluentIterable<E> filter(Predicate <? super E> predicate)
predicate - the predicate used to filter elements
NullPointerException - if predicate is null
public FluentIterable<E> limit(long maxSize)
maxSize - the maximum number of elements
IllegalArgumentException - if maxSize is negative
public FluentIterable<E> loop()
public FluentIterable<E> reverse()
public FluentIterable<E> skip(long elementsToSkip)
elementsToSkip - the number of elements to skip
IllegalArgumentException - if elementsToSkip is negative
public <O> FluentIterable<O> transform(Transformer <? super E ,? extends O> transformer)
O - the output element type
transformer - the transformer applied to each element
NullPointerException - if transformer is null
public FluentIterable<E> unique()
public FluentIterable<E> unmodifiable()
public FluentIterable<E> zip(Iterable <? extends E> other)
other - the other iterable to interleave, may not be null
NullPointerException - if other is null
public FluentIterable<E> zip(Iterable <? extends E>... others)
others - the iterables to interleave, may not be null
NullPointerException - if either of the provided iterables is null
public 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 null
NullPointerException - if predicate is null
public boolean anyMatch(Predicate<? super E> predicate)
A null or empty iterable returns false.
predicate - the predicate to use, may not be null
NullPointerException - if predicate is null
public boolean isEmpty()
public boolean contains(Objectobject)
object - the object to check
public void forEach(Closure<? super E> closure)
closure - the closure to apply to each element, may not be null
NullPointerException - if closure is null
public E get(int position)
position - the position of the element to return
IndexOutOfBoundsException - 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 elements
NullPointerException - if collection is null
public E[] toArray(Class<E> arrayClass)
arrayClass - the class of array to create
ArrayStoreException - if arrayClass is invalid
public List<E> toList()
The returned list is guaranteed to be mutable.
public StringtoString()