Java Reference
In-Depth Information
17.7
Assuming that
list
is a
List<Integer>
, explain in detail the stream pipeline:
list.stream()
.filter(value -> value %
2
!=
0
)
.sum()
17.8
Assuming that
random
is a
SecureRandom
object, explain in detail the stream pipeline:
random.ints(
1000000
,
1
,
3
)
.boxed()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.forEach((side, frequency) ->
System.out.printf(
"%-6d%d%n"
, side, frequency));
17.9
(Summarizing the Characters in a File)
Modify the program of Fig. 17.17 to summarize
the number of occurrences of every character in the file.
17.10
(Summarizing the File Types in a Directory)
Section 15.3 demonstrated how to get infor-
mation about files and directories on disk. In addition, you used a
DirectoryStream
to display the
contents of a directory. Interface
DirectoryStream
now contains
default
method
entries
, which
returns a
Stream
. Use the techniques from Section 15.3,
DirectoryStream
method
entries
, lamb-
das and streams to summarize the types of files in a specified directory.
17.11
(Manipulating a
Stream<Invoice>
)
Use the class
Invoice
provided in the
exercises
folder
with this chapter's examples to create an array of
Invoice
objects. Use the sample data shown in
Fig. 17.20. Class
Invoice
includes four properties—a
PartNumber
(type
int
), a
PartDescription
(type
String
), a
Quantity
of the item being purchased (type
int
) and a
Price
(type
double
). Per-
form the following queries on the array of
Invoice
objects and display the results:
a)
Use lambdas and streams to sort the
Invoice
objects by
PartDescription
, then display
the results.
b)
Use lambdas and streams to sort the
Invoice
objects by
Price
, then display the results.
c)
Use lambdas and streams to map each
Invoice
to its
PartDescription
and
Quantity
,
sort the results by
Quantity
, then display the results.
d)
Use lambdas and streams to map each
Invoice
to its
PartDescription
and the value of
the
Invoice
(i.e.,
Quantity
*
Price
). Order the results by
Invoice
value.
e)
Modify Part (d) to select the
Invoice
values in the range $200 to $500.
Part number
Part description
Quantity
Price
83
Electric sander
7
57.98
24
Power saw
18
99.99
7
Sledge hammer
11
21.50
77
Hammer
76
11.99
39
Lawn mower
3
79.50
68
Screwdriver
106
6.99
56
Jig saw
21
11.00
3
Wrench
34
7.50
Fig. 17.20
|
Sample data for Exercise 17.11.
17.12
(Duplicate Word Removal)
Write a program that inputs a sentence from the user (assume
no punctuation), then determines and displays the unique words in alphabetical order. Treat up-
percase and lowercase letters the same.