Java Reference
In-Depth Information
17.13
(Sorting Letters and Removing Duplicates)
Write a program that inserts 30 random letters
into a
List<Character>
. Perform the following operations and display your results:
a) Sort the
List
in ascending order.
b) Sort the
List
in descending order.
c) Display the
List
in ascending order with duplicates removed.
17.14
(Mapping Then Reducing An
IntStream
for Parallelization)
The lambda you pass to a
stream's
reduce
method should be
associative
—that is, regardless of the order in which its subexpres-
sions are evaluated, the result should be the same. The lambda expression in lines 34-36 of Fig. 17.5
is
not
associative. If you were to use parallel streams (Chapter 23, Concurrency) with that lambda,
you might get incorrect results for the sum of the squares, depending on the order in which the sub-
expressions are evaluated. The proper way to implement lines 34-36 would be
first
to map each
int
value to the square of that value,
then
to reduce the stream to the sum of the squares. Modify
Fig. 17.5 to implement lines 34-36 in this manner.