Java Reference
In-Depth Information
Table 3-1. ( continued )
Operation
Description
elementAtOrDefault
The List.get() method is used to obtain the element at the specified index if the
collection is a List . Otherwise, the elements in the collection are iterated index number
of times, and the element found at the specified position is returned.
A null is returned if the index is less than zero or greater than or equal to the number of
elements in the collection.
Example: employees.elementAtOrDefault(10)
any
Iterates over the source elements to determine if any of the elements satisfy the predicate
function test. If so, then true is returned. If no predicate function is specified, the any
operator simply returns true if the source collection is greater than zero in size.
The iteration stops as soon as the result is known.
Example: employees.any()
all
If the collection is empty, then null is returned. Otherwise all elements that satisfy the
test given by the predicate are returned. The iteration halts when the result is known.
Example: employees.all()
contains
The Collection.contains() method is invoked to obtain the result if the source implements
the collection type. Otherwise the elements of the collection are iterated to determine if the
element is found. The iteration stops as soon as a matching element is found.
Example: employees.contains(elem) // where elem is an element
Table 3-2. Eager Collection Operations
Operation
Description
where
Iterates over the source elements yielding each one for which the specified predicate
function returns true. The predicate function can accept two arguments. The first
argument of predicate function represents the element to test. The second optional
argument represents the zero-based index of the element within the source collection.
Example: employees.where(e->e.wage >= 50000)
select
Iterates over the source and yields the results of applying the selector function for each
element. The first argument of selector represents the element to process. The second
argument, if any, represents the zero-based index of the element within the source collection.
Example: employees.select(e->e.first)
selectMany
Iterates over the source elements, producing an Iterable for each element by evaluating
the selector function. If the result of evaluating the selector function is not an Iterable ,
the behavior is undefined. The inner Iterable is then iterated, and if there is no
resultSelector function then its element is yielded. If there is a resultSelector
function, it is invoked, passing the outer and inner elements as its arguments, and the
result is yielded. The second optional argument to the selector function represents the
zero-based index of the element within the source collection.
Example: employees.selectMany(e->e.position)
( continued )
 
 
Search WWH ::




Custom Search