Java Reference
In-Depth Information
Table 3-2.
(
continued
)
Operation
Description
join
Iterates over the source elements, and for each element (referred to as
outer element
),
the
outerKeySelector
function is evaluated to a value (referred to as
key1
). For each
non-null
key1
,
innerKeySequence
is iterated, and for each element (referred to as
inner
element
), the
innerKeySelector
function is evaluated to a value (referred to as
key2
).
The value
key1
is compared with
key2
, and if equal, the
resultKeySelector
function is
evaluated (with the outer element and the inner element as its arguments), and the result
is yielded.
A comparator can be used for the comparison
key1
to
key2
.
Example:
employees.join(positions, e->e.positionId, p->p.positionId,
(e, p) -> [e.first, e.last, p.position])
groupJoin
Iterates over the source elements, and for each element (referred to as
outer element
),
the
outerKeySelector
function is evaluated to a value (referred to as
key1
). If
key1
is
non-null,
innerSequence
is iterated, and for each element (referred to as
inner element
),
the
innerKeySelector
function is evaluated to a value (referred to as
key2
). The inner
elements whose
key2
equals
key1
are collected in a list. The
resultSelector
function
is then evaluated, with the outer element and the list (can possibly be empty) as its
arguments, and the result is yielded.
A comparator can be used for the comparison of
key1
and
key2
. The operator preserves
the order of the outer elements, and for each outer element, the order of the matching
inner elements is preserved.
Example:
employees.select(e ->
[e.first, e.last, e.yearsOfService.sum(y->y.years)])
concat
Iterates over the source elements, yields each element in the collection, and then iterates
over the second collection, yielding each element in the collection.
Example:
employees.concat(positions)
orderBy
,
thenBy
,
orderByDescending
,
thenByDescending
These operators can be used together to order a collection via multiple keys.
A composition of the operators has the form
source.orderBy(...).thenBy(...).thenBy(...) ...
where
orderBy(...)
is an invocation of
orderBy
or
orderByDescending
and each
thenBy(...)
, if any, is an invocation of
thenBy
or
thenByDescending
. The primary
ordering is established by the initial
orderBy
or
orderByDescending
, the first
thenBy
or
thenByDescending
establishes the secondary ordering, the second
thenBy
or
thenByDescending
establishes the tertiary ordering, and so on.
Example:
employees.orderBy(p->p.position)
thenByDescending(p->p.salary)
reverse
Iterates over the source elements, collecting all elements in a list. Each element in the
resulting list is then yielded in reverse order.
Example:
employees.reverse()
(
continued
)