Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system in Java. We'll start by building a foundational structure to handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow for filtering and aggregating user data.
In our starter task, we will implement a class that manages basic operations on a collection of user data, specifically handling adding new users, retrieving user profiles, and updating user profiles.
Here are the starter task methods:
- addUser(String userId, int age, String country, boolean subscribed)- adds a new user with the specified attributes. Returns- trueif the user was added successfully and- falseif a user with the same- userIdalready exists.
- getUser(String userId)- returns the user's profile as an object if the user exists; otherwise, returns- null.
- updateUser(String userId, Integer age, String country, Boolean subscribed)- updates the user's profile based on non-null parameters. Returns- trueif the user exists and was updated,- falseotherwise.
The Java implementation of our starter task is shown below:
The updateUser method allows for partial updates of a user's profile. Nullable parameters (age, country, and subscribed) enable selective updates, meaning only the provided non-null values are used to modify the existing user data. This design avoids unnecessary data updates and improves efficiency, especially in scenarios where only a subset of the user's data changes frequently.
With our foundational structure in place, it's time to add functionalities for filtering user data and aggregating statistics.
Here are new methods to implement:
- filterUsers(Integer minAge, Integer maxAge, String country, Boolean subscribed):- Returns the list of user IDs that match the specified criteria. Criteria can be null, meaning that the criterion should not be applied during filtering.
 
- Returns the list of user IDs that match the specified criteria. Criteria can be 
- aggregateStats()- returns statistics in the form of an object:- totalUsers: Total number of users
- averageAge: Average age of all users (rounded down to the nearest integer)
- subscribedRatio: Ratio of subscribed users to total users (as a float with two decimals)
 
This method filters users based on the criteria provided. Let's see how it works:
- The filterUsersmethod filters users based onminAge,maxAge,country, andsubscribedstatus criteria.
- It iterates over the usersmap and checks each user's profile against the provided criteria.
This method aggregates statistics from the user profiles. Let's implement it:
The aggregateStats method calculates and returns aggregate statistics about the users in the form of a Stats class object. It first determines totalUsers, the total number of users. If there are no users, it returns an object with zeroed statistics. Otherwise, it calculates  by summing the ages of all users and counts  who are subscribed. It then computes  and calculates  using integer and floating-point arithmetic. The resulting statistics object includes , , and .
Great job! Today, you've learned how to effectively handle user data by implementing advanced functionalities like filtering and aggregation on top of a basic system using Java. These are critical skills in real-life software development, where you often need to extend existing systems to meet new requirements.
I encourage you to practice solving similar challenges to solidify your understanding of data filtering and aggregation. Happy coding, and see you in the next lesson!
