...

/

HashMaps for Categorization

HashMaps for Categorization

Learn Java HashMaps by grouping meals by type (e.g., Breakfast, Lunch, Dinner).

The project

You’ve been tracking your daily meal calories, but right now they’re just numbers in an array. Wouldn’t it be more helpful to organize them by meal type, grouping all breakfasts, lunches, and dinners together?

Type the following:

Breakfast: 400
Lunch: 600
Dinner: 300

It’s convenient, but it doesn’t help you calculate totals for each category. What you need is a structure that stores data as key–value pairs so you can categorize and organize calories by meal type.

Your project: Build a Healthy Meals Tracker that groups meals into categories (Breakfast, Lunch, Dinner), calculates totals for each, and shows the overall daily total.

The programming concept

Imagine you’re making a Contacts App. If you only had an array, it might look like this:

String[] contacts = {"Alice", "123-4567", "Bob", "987-6543"};

Messy, right? You wouldn’t know which number belongs to which person.

What if you stored things as ...