A Real-World Example of a Stream Collector
See how Stream Collectors work in the wild. by
Java Stream s
Collection or a scalar. For the former, you use one of the
toXXX() method, for the latter, one of the
reducing() one.
Let s imagine an e-commerce platform that implements a shopping cart. The cart is modeled as the following:
This diagram might translate into the following (abridged) code: Java 4 5 6 8 15 4 6 10 11 14 18 22
Product; the value is the quantity.
Remember to return a read-only copy of the collection to maintain encapsulation.
Once we have defined how we store data in memory, we need to design how to display the cart on-screen. We know that the checkout screen needs to show two different bits of information: