The whole point is to use streams, so none of your code may use a loop
of any kind: no for, no enhanced for, no
while, no do/while. That includes
any helper methods you write. Each method below is also checked
individually.
Write a method that takes an IntStream and returns the sum
of the squares of its values as an int. The sum of the
squares of an empty stream is 0.
Write a method that takes an IntStream and returns the
number of different values in it as an int. The
stream 3, 1, 3, 3, 2 has three distinct values.
Write a method that takes an IntStream and returns
true if every value in it is greater than zero. An empty
stream has no values that aren't positive, so the answer for it is
true.
Write a method that takes an IntStream and returns a new
IntStream containing just the even values, in their
original order.
Write a method that takes an IntStream and returns the
second-smallest distinct value in it as an int: for
5, 2, 2, 9, 7 that is 5, since the smallest value is 2 and
the duplicate 2 doesn't count. If there are fewer than two distinct
values, return -1.
Write a method that takes an IntStream and returns the
fraction of its values that are even as a double between
0.0 and 1.0: for 1, 2, 3, 4 the answer is 0.5. The answer
for an empty stream is 0.0. (There is a way to do this with a single
pipeline—think about what average() would compute if every
even number became a 1 and every odd number a 0.)
Write a method that takes a Stream<String> and returns
the average length of the strings in it as a double, or 0.0
if the stream is empty.
Write a method that takes a Stream<String> and returns
the longest string in it. If several strings are tied for longest, return
the one that comes first in the stream. If the stream is empty, return
the empty string "".
Write a method that takes two arguments, a Stream<String>
and an int, and returns true if any string in
the stream is longer than the int.
Write a method that takes a Stream<String> of
non-empty strings and returns a String made of the first
letter of each one, in order: for "berkeley", "high",
"school" the answer is "bhs". For an empty stream,
return "".
Write a method that takes a Stream<String> and returns
a List<String> of its strings sorted from shortest to
longest. Strings of the same length should stay in their original order
relative to each other. (Have a look at
Comparator.comparingInt.)
Write a method that takes an int n and returns an
IntStream of the squares of the numbers from 1 to n,
in order: for 4 that is 1, 4, 9, 16. For 0 (or any smaller
number) return an empty stream.
Write a method that takes a String made up of digits, such
as "1234", and returns the sum of those digits as an
int: for "1234" that is 10, and for the empty
string it is 0. (Hint: a String has a
codePoints() method that gives you an IntStream
of its code points, and Character.digit converts a single
code point to its digit value.)
In the last streams assignment you re-solved old array problems with
streams. This time the streams are the starting point: most of these
methods are handed a stream—an IntStream of numbers or
a Stream<String> of words—and have to compute something
from it. A few return a stream of their own, and a couple build a stream
from a single number.
Remember that a stream can only be consumed once: once you have called a
terminal operation such as sum(), count(),
toArray(), or collect() on it, it is used up.
Most of these methods can be written as a single pipeline from the argument
to the answer. If you really need to look at the values twice, collect
them into an array or a List first and stream that.
The starter file imports java.util.* and
java.util.stream.*, which is everything you need:
IntStream, Stream, Collectors,
Comparator, and List.