Course Content
Stream API
Stream API
1. Fundamentals and Functional Capabilities of Stream API
ArchitecturePrinciplesWhat Is a Functional Interface?Predicate: Data FilteringChallenge: Filtering Corporate Email AddressesFunction: Data TransformationChallenge: Income Tax CalculationComparable: Natural Ordering of DataComparator: Custom Comparison of DataChallenge: Sorting Employees Consumer: Processing Data Supplier: Data GenerationBi-versions of Functional InterfacesChallenge: Filtering Users by Two CriteriaBinaryOperator: Combining Two Values
2. Intermediate Operations in Stream API
Transforming Elements with the map() MethodFiltering Elements with the filter() MethodChallenge: Factory Product FilteringWorking with Nested Structures with the flatMap() MethodArranging Items in Order with the sorted() MethodChallenge: Selecting the Best Cars on the Production LineEliminating Duplicates with the distinct() MethodChallenge: Factory Quality ControlRestricting and Skipping Elements with the limit() and skip() MethodsChallenge: Finding the Top 3 Hardest-Working EmployeesIntermediate Processing with the peek() Method
3. Terminal Operations in the Stream API
collect() Gathering Stream Elements into a CollectionChallenge: Build a Custom Collector for Category CountingCollectors Utility Class for Stream APIProcessing Elements with the forEach() MethodHandling Values with the Optional ClassAggregating Elements with the reduce() MethodChallenge: Calculating Total Cost with Discounts and TaxCalculating Stream Statistics with count(), max(), and min()Retrieving Stream Summary Metrics with summaryStatistics() MethodRetrieving Elements from a Stream with findFirst() and findAny()Challenge: Selecting Random Products Within a CategoryChecking Stream Elements Against a Condition with allMatch()Challenge: Ensuring Fast Delivery for Expensive Products
4. Practical Applications of Stream API
Challenge: Calculating Total Cost with Discounts and Tax
Task
Swipe to start coding
Implement a shopping cart system that calculates the total order cost, applying a discount and sales tax based on predefined conditions.
- Use
map()
to get the total price of each product (there is a dedicated methodgetTotalPrice()
in theProduct
class). - Apply
reduce()
to sum up all product prices. - Use a ternary operator to check if the
subtotal
exceeds the discount threshold (DISCOUNT_THRESHOLD
) and calculate thediscount
. Ifsubtotal
is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE
); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotal
byDISCOUNT_PERCENTAGE
. - Calculate the pre-tax amount (
preTaxAmount
) by subtractingdiscount
fromsubtotal
. - Compute the sales tax (
tax
) by multiplyingpreTaxAmount
by the sales tax rate (SALES_TAX
). - Return the final total, calculated as
preTaxAmount + tax
.
Solution
solution
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 7
Challenge: Calculating Total Cost with Discounts and Tax
Task
Swipe to start coding
Implement a shopping cart system that calculates the total order cost, applying a discount and sales tax based on predefined conditions.
- Use
map()
to get the total price of each product (there is a dedicated methodgetTotalPrice()
in theProduct
class). - Apply
reduce()
to sum up all product prices. - Use a ternary operator to check if the
subtotal
exceeds the discount threshold (DISCOUNT_THRESHOLD
) and calculate thediscount
. Ifsubtotal
is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE
); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotal
byDISCOUNT_PERCENTAGE
. - Calculate the pre-tax amount (
preTaxAmount
) by subtractingdiscount
fromsubtotal
. - Compute the sales tax (
tax
) by multiplyingpreTaxAmount
by the sales tax rate (SALES_TAX
). - Return the final total, calculated as
preTaxAmount + tax
.
Solution
solution
Everything was clear?
Thanks for your feedback!
Section 3. Chapter 7