Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 2.45 KB

ex07.md

File metadata and controls

61 lines (44 loc) · 2.45 KB

Exercise 07

Part A

Consider the class org.pg4200.ex07.ComputationExampleTraditional, with passing test cases in org.pg4200.ex07.ComputationExampleTraditionalTest. You need to implement the same functionality, but by using only streams and lambdas. In particular, develop a class called ComputationExampleStream that implements the interface org.pg4200.ex07.ComputationExample. The implemented method should start with return books.stream() and end with .collect(Collectors.toList());. You need to use the right methods in between, like for example filter, map, flatMap and distinct.

Write a test class ComputationExampleStreamTest that extends
org.pg4200.ex07.ComputationExampleTestTemplate. If your implementation is correct, all tests should pass.

Part B

Consider the interface org.pg4200.ex07.AnotherStream, which defines a series of new stream and terminal operators. Implement a class called AnotherStreamList that extends MyIterableLinkedList and that has the following method:

public AnotherStream<T> stream()

When implementing AnotherStreamList, you can reuse code parts from MyStreamSupport. For example, the count() terminal operation has some similarities with collectToList(), in which you will need to implement your own Consumer to count the number of elements that go through the stream.

Write a test class AnotherStreamTest to verify that your implementation of AnotherStream is correct.

Part C

Consider the interface org.pg4200.ex07.ExtendedList, which defines the same kind of stream and terminal operations seen in class. However, instead of using streams, the operations create new list instances. Write a class ExtendedListImpl that extends MyArrayList and implements ExtendedList.

Write a test class ExtendedListImplTest that extends org.pg4200.ex07.ExtendedListTestTemplate. If your implementation is correct, all tests should pass.

After doing this exercise, ponder on why we need streams. For example, if we were using only ExtendedList, what would be the performance drawbacks of chaining calls?

Part D

Study the source code of MyStreamSupport. Once you think you fully understand it, write its implementation on paper (e.g., using a pen), without looking at the source code. Once done, compare what you wrote with the actual implementation.

Solutions

Solutions to this exercise can be found in the solutions module, under the org.pg4200.sol07 package.