Java Reference

In-Depth Information

Table 3-2.
(
continued
)

Operation

Description

sequenceEqual

Iterates over the two source elements in parallel and compares their corresponding

elements. If all corresponding elements are equal and the collection sizes are equal, then

true
is returned. Otherwise, the method returns
false
.

If a comparator is specified, it is used to compare the elements.

Example:
seqA.sequenceEqual(seqB)

defaultIfEmpty

Iterates over and yields all elements of the source collection. If the source collection is

empty, the
defaultValue
is yielded. If
defaultvalue
is not specified, a
null
is yielded.

Example:
employees.defaultIfEmpty()

count

Collection.size()
is used to obtain the count if there is no predicate function specified

and if the source implements
Collection
. If the source does not implement
Collection
,

then the count operator iterates over the source elements, counting the total number.

If a predicate is specified, the
count
operator iterates over the source elements and

counts the number of elements for which the predicate function returns
true
.

Example:
employees.count()

sum

Iterates over the source elements, invoking the selector function for each element.

Computes the sum of the returned values. If no selector function is specified, the sum of

the elements in the collection is computed.

Zero is returned for an empty collection. Furthermore, the operator does not include

null
values in the result.

Example:
[3,5,7,10].sum()

min

Iterates over the source elements, invoking the selector function for each element. Finds

the minimum of the returned values. If no selector function is specified, the minimum of

the elements in the collection is computed. The values to be compared must implement

Comparable
.

null
is returned for an empty collection.

Example:
[3,5,7,10].min() // returns 3

max

Iterates over the source elements, invoking the selector function for each element. Finds

the maximum of the returned values. If no selector function is specified, the maximum of

the elements in the collection is computed. The values to be compared must implement

Comparable
.

The
max
operator returns
null
for an empty collection.

Example:
[3,5,7,10].max() // returns 10

average

Iterates over the source elements, invoking the selector function for each element.

Computes the average of the returned values. If no selector function is specified, the

average of the elements in the collection is calculated.

The
average
operator returns
null
for an empty collection.

Example:
[3,5,7].average()

(
continued
)