Java Reference
In-Depth Information
Supporting Operations
A collection of data is represented in EL as java.lang.Iterable<Object> . Collections of data can be iterated over,
and EL provides operations to perform on data while iterating over it. There are operations that can be performed on
collections to accomplish a myriad of tasks, including filtering, sorting, selecting a subset of elements, and so forth.
In this section, you will learn each of the collection operations and how they work.
There are two different types of collection operations: lazy and eager. Lazy operations tend to iterate only
when necessary, and they avoid the creation of new collections (Table 3-1 ). Eager operations iterate over the entire
collection each time they are performed (Table 3-2 ). The following sections break the collection operations into each
of these two categories.
Table 3-1. Lazy Collection Operations
Operation
Description
take
Iterates over the source elements and yields the number of elements specified by the
given argument. Elements are yielded from the front of the collection. If the count is
greater than the number of source elements, all the elements are yielded; otherwise, if the
count is less than or equal to zero, no elements are yielded.
Example: employees.take(2)
skip
Iterates over the source elements, skipping the number of elements specified by the
argument and yielding all of the remaining elements. If the source collection contains
fewer elements than the number specified by the argument, nothing is yielded. If count is
less than or equal to zero, all elements are yielded.
Example: employees.skip(2)
takeWhile
Iterates over the source elements, applying the given predicate function to each element
and yielding the elements that return a true result. The iteration stops the first time the
predicate function returns false or when all of the elements have been processed. The
predicate function takes two arguments. The first argument represents the element to
test. The second argument, if present, represents the zero-based index of the element
within the source collection.
Example: employees.takeWhile(Boolean predicate)
skipWhile
Iterates over the source elements, applying the given predicate function to each element
and skipping those elements that return a true result. The first element that returns a
false result is yielded, along with all remaining elements. No elements are yielded if
the predicate function returns true for all elements. The predicate function takes two
arguments. The first argument represents the element to test. The second argument,
if present, represents the zero-based index of the element within the source sequence.
Example: employees.skipWhile(Boolean predicate)
first
Iterates the source elements, and if a predicate function is specified, then the first
element returning a true result is yielded. Otherwise, if no predicate function is specified,
the first operator simply returns the first element in the collection.
An InvalidOperationException is thrown if no element matches the predicate or if the
source collection is empty.
Example: employees.first()
( continued )
 
 
Search WWH ::




Custom Search