Vous êtes sur la page 1sur 3

Homework Assignments - Week 8 (streams)

0. Warm-up
a. Given a list of Strings, write a method that returns a list of all strings
that start with the letter 'a' (lower case) and have exactly 3 letters.

b. Write a method that returns a comma separated string based on a


given list of integers. Each element should be preceded by the letter
'e' if the number is even, and preceded by the letter 'o' if the number
is odd. For example, if the input list is (3,44), the output should be
'o3,e44'.

c. Given a string, count the occurrences of each letter (hint: see


Collectors.groupingBy, Collectors.counting..)

d. Print the first 10 powers of 3 which are greater than 1000 (hint: use
IntStream.iterate(), limit(), filter(), boxed()..)
Output should be like: 2187, 6561, ...

e. Print all the square numbers (of form n^2) between 1000 and 10000
(hint: use IntStream.range(), map(), boxed()..)
Output should be like: 1024, 1089, 1156, ....

f. Solve again the problems from Week 5 (Collections), but using


stream operations whenever possible - the problems:
- “Warm-up” (counting words)
- “Buildings Registry”
- “Persons Registry”.
1. Set operations with Streams
Given 2 Set<T> instances (holding elements of a generic type T), find
the intersection, union, and difference between them, but only using
stream operations.

2. FizzBuzz with Streams


Write 2 solutions to the the FizzBuzz problem:
- First a regular one (imperative style, with loops, ifs, etc)
- Then one only with streams and lambdas (hint: see
IntStream.rangeClosed to simulate a stream of numbers)

The FizzBuzz problem: Given a range of numbers, for each number,


display: fizz, if the number divides by 3, buzz if it divides by 5,
fizzbuzz if it divides by both or the number itself if none of the above.

3. Trader Transactions
Create 2 classes:
- Trader​ - with properties: name, city
- Transaction​ - with properties: year, value, trader

Compute the following based on a list of such transactions:


● Find all transactions from 2011 and sort them by value (small to
high)
● What are all the unique cities where traders work?
● Find all traders from Cambridge and sort them by name
(descending)
● Return a string of all traders’ names sorted alphabetically and
separated by comma
● Determine if there are any traders from Milan
● Update all transactions so that the traders from Milan are
moved to Cambridge
● What’s the highest value in all transactions?
● What’s the transaction with the lowest value?

Vous aimerez peut-être aussi