Lesson 4
Applying HashMaps to Real-World Challenges in Java
Introduction and Goal Setting

Hello there! In this lesson, we will apply HashMaps to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Scenarios Calling for HashMaps

HashMaps are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency with some actual examples.

Solving Real-World Task 1: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.

This approach allows us to add, search for, and remove books from our library catalog using just a few lines of Java code.

Java
1import java.util.HashMap; 2import java.util.Map; 3 4class Solution { 5 public static void main(String[] args) { 6 // Initializing a HashMap 7 HashMap<String, HashMap<String, String>> libraryCatalog = new HashMap<>(); 8 9 // Details of a book 10 String bookId = "123"; 11 // Creating a HashMap to store details of the book 12 HashMap<String, String> bookDetails = new HashMap<>(); 13 bookDetails.put("title", "To Kill a Mockingbird"); 14 bookDetails.put("author", "Harper Lee"); 15 bookDetails.put("year_published", "1960"); 16 17 libraryCatalog.put(bookId, bookDetails); // Adding a book to library catalog, where value itself is a HashMap 18 19 // Searching for a book 20 if (libraryCatalog.containsKey(bookId)) { 21 System.out.println("Title: " + libraryCatalog.get(bookId).get("title") + 22 ", Author: " + libraryCatalog.get(bookId).get("author") + 23 ", Year Published: " + libraryCatalog.get(bookId).get("year_published")); 24 } 25 26 libraryCatalog.remove(bookId); // Removing a book from the library 27 } 28}

As you can see, HashMaps make the task of cataloging books in the library simpler and more efficient!

Solving Real-World Task 2: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a HashMap, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some Java code to better understand this.

Java
1import java.util.HashMap; 2import java.util.Map; 3import java.util.List; 4import java.util.Arrays; 5 6class Solution { 7 public static void main(String[] args) { 8 List<String> votesList = Arrays.asList("Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"); // Cast votes 9 HashMap<String, Integer> voteCounts = new HashMap<>(); // Initializing a HashMap 10 11 // Counting the votes 12 for (String name : votesList) { 13 voteCounts.put(name, voteCounts.getOrDefault(name, 0) + 1); 14 } 15 16 for (Map.Entry<String, Integer> entry : voteCounts.entrySet()) { 17 System.out.println(entry.getKey() + ": " + entry.getValue()); 18 } 19 // Prints: Alice: 3, Bob: 2, Charlie: 1 20 } 21}

HashMaps facilitate the efficient counting of votes.

Solving Real-World Task 3: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a HashMap in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

Java
1import java.util.HashMap; 2import java.util.Map; 3 4class Solution { 5 public static void main(String[] args) { 6 HashMap<String, Integer> storeInventory = new HashMap<>(); 7 storeInventory.put("Apples", 100); 8 storeInventory.put("Bananas", 80); 9 storeInventory.put("Oranges", 90); // Initializing an inventory 10 11 storeInventory.put("Pears", 50); // Adding a new product to inventory and setting its quantity 12 13 storeInventory.put("Apples", storeInventory.get("Apples") + 20); // Updating the number of apples in inventory 14 15 String prod = "Apples"; // A product to be checked 16 System.out.println("Total " + prod + " in stock: " + storeInventory.get(prod)); 17 18 // Check if a product is in stock 19 prod = "Mangoes"; 20 if (storeInventory.containsKey(prod)) { // If mangoes exist in inventory 21 System.out.println(prod + " are in stock."); 22 } else { // If mangoes don't exist in inventory 23 System.out.println(prod + " are out of stock."); 24 } 25 } 26}

Thus, when managing inventory data, HashMaps offer an efficient solution!

Lesson Summary and Practice

In this lesson, we bridged the gap between the theory of HashMaps and their practical applications. We explored real-world problems that can be solved using HashMaps and implemented Java code to address them.

Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your HashMap problem-solving skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.