From the course: Java Practice: Functional Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Inspect a collection

Solution: Inspect a collection - Java Tutorial

From the course: Java Practice: Functional Programming

Solution: Inspect a collection

- This challenge starts with a data set a list of items where each item has an ID, a name, a price, and a quantity. Your job in the challenge is to calculate the total value of the shopping cart using functional programming. Here's my solution to the challenge. In my getCartTotal method, I'm starting by wrapping the value of zero inside an instance of atomic reference. I'm doing this because I'm going to be passing this value into a lambda expression, and if I simply try to pass a primitive value, say a float or an int, or even one of the wrapper objects, like Float with an uppercase F into the lambda, I won't be able to modify it because that value would have to be marked as final. So I wrap that value inside an atomic reference object and that object will be final, but the value in it can be updated. Now I'm receiving a list of items and I need to iterate through that list, so I'm going to call the forEach method. This…

Contents