Mastering Data Aggregation and Data Streams in Java

Welcome to our lesson on mastering data aggregation and data streams in Java. In this lesson, we'll start by building a basic sales records aggregator. Then, we'll extend its functionality to handle more complex operations such as filtering, data aggregation, and formatted outputs. By the end of this session, you'll be able to manage and format data streams efficiently using Java.

Starter Task Methods

To begin, we'll implement a basic sales record aggregator. Here are the methods we'll be focusing on:

  • addSale(int saleId, double amount) - Adds a sale record with a unique identifier saleId and an amount. If a sale with the same saleId already exists, it updates the amount.
  • getSale(int saleId) - Retrieves the sale amount associated with the saleId. If the sale does not exist, it returns null.
  • deleteSale(int saleId) - Deletes the sale record with the given saleId. Returns true if the sale was deleted and false if the sale does not exist.

Are these methods clear so far? Great! Let's now look at how we would implement them.

Starter Task Implementation

Here is the complete code for the starter task:

Explanation:

  • The SalesAggregator class initializes an empty HashMap to store sales records.
  • The addSale method adds a new sale or updates the amount for an existing sale ID.
  • The getSale method retrieves the amount for a given sale ID or returns null if the sale does not exist.
  • The deleteSale method removes the sale record for the given sale ID or returns false if the sale does not exist.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

New Methods and Their Definitions

To increase the complexity and usefulness of our sales aggregator, we'll adjust some existing methods and introduce some new methods and functionalities involving formatted outputs.

  • addSale(int saleId, double amount, LocalDateTime date) - Adds or updates a sale record with a unique identifier saleId, amount, and a date.
  • aggregateSales(double minAmount) - Returns an object with the total number of sales and the total amount of sales where the sale amount is above minAmount. The object format looks like this:
  • formatSales(double minAmount) - Returns the sales data, filtered by minAmount, formatted as JSON. Includes aggregated sales statistics in the output.
  • getSalesInDateRange(LocalDateTime startDate, LocalDateTime endDate) - Retrieves all sales that occurred within the given date range, inclusive. Each sale includes saleId, amount, and date.

Let's implement these methods step-by-step.

Step 1: Enhancing the addSale Method to Include Date

We'll first modify the addSale method to accept a date.

This ensures that each sale record includes a date in addition to the amount.

Step 2: Implementing the aggregateSales Method

Now, we create the aggregateSales method:

This method iterates through the sales and sums up those that exceed the minAmount.

Step 3: Implementing the formatSales Method

Next, we'll create the formatSales method to output data in JSON format using Gson.

This function utilizes Gson, a Java library from Google that converts Java objects to JSON representations, to format sales data as JSON, and includes aggregated statistics.

Step 4: Implementing the getSalesInDateRange Method

Finally, let's implement the getSalesInDateRange method:

This method retrieves all sales within the specified date range.

Summary

Congratulations! You've now extended a basic sales aggregator to an advanced one capable of filtering, aggregating, and providing formatted outputs in Java. These skills are crucial for handling data streams efficiently, especially when dealing with large datasets. Feel free to experiment with similar challenges to reinforce your understanding. Well done, and see you in the next lesson!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal