Activity: Master the HashMap Data Structure

Deadline: October 4, 2024
Activity Type: Coding
Goal: Practice using HashMaps with different Java classes.

In this activity, you will apply your understanding of data structures, specifically the HashMap.

This is a follow-up to Activity 16

Check this:

ACTIVITY (hashnode.space)

where you learned about the list, object, and list of objects. Now, you will take this knowledge and apply it to a more advanced data structure—HashMap.

You are required to create 5 Java classes and use HashMap to store and retrieve data from these objects. The goal is to familiarize yourself with HashMap operations such as adding data, accessing data using keys, and printing the data.

use eclipse, IntelliJ, or Visual Studio code

Instructions:

  1. Create the following Java classes:

    • Product: For storing product details (e.g., product name, price).

    • Employee: For storing employee details (e.g., name, job title).

    • Books: For storing book details (e.g., title, author).

    • University: For storing university details (e.g., name, location).

    • Restaurant: For storing restaurant details (e.g., name, cuisine type).

  2. Use HashMap to store the objects for each class:

    • The key for each HashMap should be an integer (such as an ID).

    • The value should be an instance of the corresponding class.

  3. Perform the following tasks:

    • Create a HashMap for each class and add at least 3 entries per class.

    • Use the HashMap.get() method to retrieve and print the details of each object.


Sample Structure:

Example for Student Class:

import java.util.HashMap;

public class StudentObjectsExample {
    public static void main(String[] args) {
        // Student 1
        HashMap<String, Object> student_object1 = new HashMap<>();
        student_object1.put("name", "Juan Carlos");
        student_object1.put("section", "BSIT-4B");
        student_object1.put("id", 1);
        student_object1.put("email", "juan@gmail.com");
        student_object1.put("age", 22);

        // Student 2
        HashMap<String, Object> student_object2 = new HashMap<>();
        student_object2.put("name", "Jose Rizal");
        student_object2.put("section", "BSIT-2A");
        student_object2.put("id", 2);
        student_object2.put("email", "jose@gmail.com");
        student_object2.put("age", 21);

        // Student 3
        HashMap<String, Object> student_object3 = new HashMap<>();
        student_object3.put("name", "Juan Luna");
        student_object3.put("section", "BSIT-3A");
        student_object3.put("id", 3);
        student_object3.put("email", "juanl@gmail.com");
        student_object3.put("age", 20);

        // Student 4
        HashMap<String, Object> student_object4 = new HashMap<>();
        student_object4.put("name", "Andres Bonifacio");
        student_object4.put("section", "BSIT-3A");
        student_object4.put("id", 4);
        student_object4.put("email", "andres@gmail.com");
        student_object4.put("age", 20);

        // Student 5
        HashMap<String, Object> student_object5 = new HashMap<>();
        student_object5.put("name", "Justin Bieber");
        student_object5.put("section", "BSIT-2A");
        student_object5.put("id", 5);
        student_object5.put("email", "justin@gmail.com");
        student_object5.put("age", 21);

        // Student 6
        HashMap<String, Object> student_object6 = new HashMap<>();
        student_object6.put("name", "Michael Jordan");
        student_object6.put("section", "BSIT-4A");
        student_object6.put("id", 6);
        student_object6.put("email", "michael@gmail.com");
        student_object6.put("age", 19);

        // Student 7
        HashMap<String, Object> student_object7 = new HashMap<>();
        student_object7.put("name", "Andrew Jordan");
        student_object7.put("section", "BSIT-4A");
        student_object7.put("id", 7);
        student_object7.put("email", "andrew@gmail.com");
        student_object7.put("age", 19);

        // Student 8
        HashMap<String, Object> student_object8 = new HashMap<>();
        student_object8.put("name", "Jessa Boe");
        student_object8.put("section", "BSIT-2B");
        student_object8.put("id", 8);
        student_object8.put("email", "jessa@gmail.com");
        student_object8.put("age", 18);

        // Student 9
        HashMap<String, Object> student_object9 = new HashMap<>();
        student_object9.put("name", "Ted Talk");
        student_object9.put("section", "BSIT-3B");
        student_object9.put("id", 9);
        student_object9.put("email", "ted@gmail.com");
        student_object9.put("age", 19);

        // Using get() method to display the information of each student
        System.out.println("Student 1: " + student_object1.get("name") + ", Section: " + student_object1.get("section") + ", Email: " + student_object1.get("email"));
        System.out.println("Student 2: " + student_object2.get("name") + ", Section: " + student_object2.get("section") + ", Email: " + student_object2.get("email"));
        System.out.println("Student 3: " + student_object3.get("name") + ", Section: " + student_object3.get("section") + ", Email: " + student_object3.get("email"));
        System.out.println("Student 4: " + student_object4.get("name") + ", Section: " + student_object4.get("section") + ", Email: " + student_object4.get("email"));
        System.out.println("Student 5: " + student_object5.get("name") + ", Section: " + student_object5.get("section") + ", Email: " + student_object5.get("email"));
        System.out.println("Student 6: " + student_object6.get("name") + ", Section: " + student_object6.get("section") + ", Email: " + student_object6.get("email"));
        System.out.println("Student 7: " + student_object7.get("name") + ", Section: " + student_object7.get("section") + ", Email: " + student_object7.get("email"));
        System.out.println("Student 8: " + student_object8.get("name") + ", Section: " + student_object8.get("section") + ", Email: " + student_object8.get("email"));
        System.out.println("Student 9: " + student_object9.get("name") + ", Section: " + student_object9.get("section") + ", Email: " + student_object9.get("email"));
    }
}

Explanation:

  • The get() method is used to retrieve values from each student's HashMap.

  • For each student, the name, section, and email are retrieved and printed.

  • You can modify or add more get() statements if you need to access other data like age, ID, etc.

You will create similar code for the other classes (Product, Employee, Books, University, Restaurant).


Tasks:

  1. Define classes for Product, Employee, Books, University, and Restaurant.

  2. Create HashMaps for each class, using an integer key and object as the value.

  3. Add 3 entries for each class in the corresponding HashMap.

  4. Retrieve and print the entries using the get() method.


Submission:


Deadline:

The submission is due on October 4, 2024.

Updated on