Search⌘ K
AI Features

HashMaps for Categorization

Explore how to organize and categorize data using HashMaps in Java. Learn to store meal and exercise information as key-value pairs, calculate category totals, and implement a Healthy Meals Tracker and Exercise Tracker to apply these concepts practically.

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:

C++
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 ...