Streams Assignment
Streams Assignment
These exercises are based on question id’s (QID’s) from Enthuware, the excellent Java Certification training
tool. Enthuware is an MCQ tool and this is why some answers in the video refer to “options”. However, I
have left these discussions in the solutions video as it enables me to discuss extra material, which aids in the
learning experience. Note: the QID’s are from Java 8 OCP question bank.
1. Stream a list of int primitives between the range of 0 (inclusive) and 5 (exclusive). Calculate and
output the average. (QID 2.2023)
2. Given the Item class (in the zip file), declare a List typed for Item with the following Item’s:
a. id=1 name=”Screw”
b. id=2 name=”Nail”
c. id=3 name=”Bolt”
Stream the list and sort it so that it outputs “BoltNailScrew” i.e. alphabetic name order. Use Stream’s
forEach method to output the names (use the method reference version for the required Consumer
lambda). (QID Q2.1762)
Page 1 of 4
5. Code a method public static Optional<String> getGrade(int marks) (QID 2.1826)
a. in the method getGrade:
i. declare an empty optional, typed for String called grade
ii. insert the following code:
if (marks > 50) {grade = Optional.of(“PASS”);} else {grade.of(“FAIL”);}
b. in main():
i. declare an Optional, typed for String named grade1 which is initialised to the return
value of calling getGrade(50)
ii. declare an Optional, typed for String named grade2 which is initialised to the return
value of calling getGrade(55)
iii. using orElse() on grade1, output the value of grade1 or “UNKNOWN”
iv. if(grade2.isPresent()) is true: use ifPresent(Consumer) to output the contents of
grade2; if false, use orElse() to output the contents of grade2 or “Empty”
v. Notes:
1. Optional’s are immutable.
2. Optional.of(null); // NullPointerException
3. Optional.ofNullable(null); // Optional.empty returned
6. Given the Book class (in the zip file), declare a List typed for Book with the following Book’s:
a. title=”Thinking in Java”, price=30.0
b. title=”Java in 24 hrs”, price=20.0
c. title=”Java Recipes”, price=10.0
Stream the books and calculate the average price of the books whose price is > 10.
Change the filter to books whose price is > 90. Ensure you do not get an exception. (QID 2.1809)
7. Given the Book class (in the zip file), declare a List typed for Book with the following Book’s:
a. title=”Atlas Shrugged”, price=10.0
b. title=”Freedom at Midnight”, price=5.0
c. title=”Gone with the wind”, price=5.0
Stream the books and instantiate a Map named ‘bookMap’ that maps the book title to its price. To do
this use the collect(Collectors.toMap(Function fnToGetKey, Function fnToGetValue)). Iterate
through ‘bookMap’ (using the Map forEach(BiConsumer) method). The BiConsumer only outputs
prices where the title begins with “A”. (QID 2.1846)
8. Given the Book class (in the zip file), declare a List typed for Book with the following Book’s:
a. title=”Gone with the wind”, price=5.0
b. title=”Gone with the wind”, price=10.0
c. title=”Atlas shrugged”, price=15.0
Page 2 of 4
9. Given the Person class (in the zip file), declare a List typed for Person with the following Person’s:
a. name=”Bob”, age=31
b. name=”Paul”, age=32
c. name=”John”, age=33
Pipeline the following where the return type is double: (QID 2.1810)
stream the people
filter the stream for Person’s whose age is < 30
map to int primitives
calculate the average age.
This should generate a NoSuchElementException. Using orElse(), fix the pipeline (not the filter) so
that 0.0 is returned instead of an exception being generated.
11. Given the AnotherBook class (in the zip file), declare a List typed for AnotherBook namely ‘books’
with the following AnotherBook’s:
a. title=”Gone with the wind”, genre=”Fiction” (QID 2.1858)
b. title=”Bourne Ultimatum”, genre=”Thriller”
c. title=”The Client”, genre=”Thriller”
14. Examine the following code. Note that an AtomicInteger is a version of Integer that is safe to use in
concurrent (multi-threaded) environments. The method incrementAndGet() is similar to ++ai
a) Why is the value of ai at the end 0 (and not 4)? (QID 2.1841)
Page 4 of 4