Lesson 4
Compound Data Structures in Java
Compound Data Structures in Java

Welcome to our exploration of Compound Data Structures in Java. Having navigated through Maps, Sets, and Arrays, we'll delve into nested HashMaps and arrays. These structures enable us to handle complex and hierarchical data, which is typical in real-world scenarios. Nested data structures are commonly used to represent data models like organizational charts, product categories, and multi-dimensional datasets. This lesson will guide you through a recap of the basics, as well as the creation and modification of nested HashMaps and arrays.

Recap: Maps, Arrays, and Understanding Nested Structures

As a quick recap, Arrays are mutable, ordered collections, while HashMaps are collections of key-value pairs with unique keys. These structures can be nested. Here's a simple example of a school directory:

Java
1import java.util.HashMap; 2 3public class Solution { 4 public static void main(String[] args) { 5 // HashMap with grades as keys and arrays of students as values 6 HashMap<String, String[]> schoolDirectory = new HashMap<>(); 7 schoolDirectory.put("Grade1", new String[] { "Amy", "Bobby", "Charlie" }); 8 schoolDirectory.put("Grade2", new String[] { "David", "Eve", "Frank" }); 9 schoolDirectory.put("Grade3", new String[] { "George", "Hannah", "Ivy" }); 10 11 // Logs the Grade1 array in the HashMap 12 System.out.println(String.join(", ", schoolDirectory.get("Grade1"))); // Output: Amy, Bobby, Charlie 13 } 14}

In this example, we have a HashMap where each key represents a grade, and the corresponding value is an array of student names. This is a simple demonstration of a nested data structure.

Creating Nested HashMaps and Arrays

Just like their non-nested versions, creating nested structures is straightforward.

Nested HashMap:

Java
1import java.util.HashMap; 2import java.util.Map; 3 4public class Solution { 5 public static void main(String[] args) { 6 // HashMap within a HashMap 7 HashMap<String, HashMap<String, String>> nestedMap = new HashMap<>(); 8 nestedMap.put("fruit", new HashMap<>() {{ 9 put("apple", "red"); 10 put("banana", "yellow"); 11 }}); 12 nestedMap.put("vegetable", new HashMap<>() {{ 13 put("carrot", "orange"); 14 put("spinach", "green"); 15 }}); 16 17 // Logs the nested map 18 for (Map.Entry<String, HashMap<String, String>> category : nestedMap.entrySet()) { 19 System.out.println(category.getKey() + ":"); 20 for (Map.Entry<String, String> item : category.getValue().entrySet()) { 21 System.out.println(" " + item.getKey() + ": " + item.getValue()); 22 } 23 } 24 } 25}

Here, we have a HashMap that contains other HashMaps as values. Each top-level key, such as "fruit" or "vegetable," points to another HashMap that holds key-value pairs related to that top-level category.

We utilize double-brace initialization to populate these nested HashMaps efficiently. This technique involves creating an anonymous inner subclass of HashMap (first brace: new HashMap<>() { ... }) and using an instance initializer block (second brace: { ... }) to add key-value pairs like put("apple", "red") immediately at runtime, in one step.

Nested Array:

Java
1import java.util.Arrays; 2 3public class Solution { 4 public static void main(String[] args) { 5 // Arrays within an array 6 int[][] nestedArray = { 7 { 1, 2, 3 }, 8 { 4, 5, 6 }, 9 { 7, 8, 9 } 10 }; 11 12 // Logs the nested array 13 for (int[] innerArray : nestedArray) { 14 System.out.println(Arrays.toString(innerArray)); 15 } 16 } 17}

In this case, we create a nested array where each element of the outer array is itself an array. This structure is useful for scenarios like multi-dimensional datasets.

Nested HashMaps and Arrays:

Java
1import java.util.HashMap; 2import java.util.Map; 3import java.util.Arrays; 4 5public class Solution { 6 public static void main(String[] args) { 7 // Arrays within a HashMap 8 HashMap<String, int[]> arrayMap = new HashMap<>(); 9 arrayMap.put("numbers", new int[] { 1, 2, 3 }); 10 arrayMap.put("letters", new int[] { 10, 11, 12 }); 11 12 // Logs the HashMap of arrays 13 for (Map.Entry<String, int[]> pair : arrayMap.entrySet()) { 14 System.out.println(pair.getKey() + ": " + Arrays.toString(pair.getValue())); 15 } 16 } 17}

This example shows a HashMap where each value is an array. This pattern is practical for mapping categories to lists of values efficiently.

Accessing Values in Nested Structures

The retrieval of values from nested HashMaps or arrays follows rules similar to those for their non-nested counterparts.

From Nested HashMap:

Java
1import java.util.HashMap; 2 3public class Solution { 4 public static void main(String[] args) { 5 // HashMap within a HashMap 6 HashMap<String, HashMap<String, String>> nestedMap = new HashMap<>(); 7 nestedMap.put("fruit", new HashMap<>() {{ 8 put("apple", "red"); 9 put("banana", "yellow"); 10 }}); 11 nestedMap.put("vegetable", new HashMap<>() {{ 12 put("carrot", "orange"); 13 put("spinach", "green"); 14 }}); 15 16 // Accessing apple's color from the nested HashMap 17 System.out.println(nestedMap.get("fruit").get("apple")); // Output: red 18 } 19}

Here, we navigate the nested HashMap structure by chaining the key lookups. First, we access the "fruit" HashMap, then the "apple" key within it.

From Nested Array:

Java
1public class Solution { 2 public static void main(String[] args) { 3 // Arrays within an array 4 int[][] nestedArray = { 5 { 1, 2, 3 }, 6 { 4, 5, 6 }, 7 { 7, 8, 9 } 8 }; 9 10 // Accessing the 3rd value from the 2nd array in the nested array 11 System.out.println(nestedArray[1][2]); // Output: 6 12 } 13}

In the nested array, we access elements using double indexing. Here, nestedArray[1][2] retrieves the third element of the second array.

From Nested HashMaps and Arrays:

Java
1import java.util.HashMap; 2 3public class Solution { 4 public static void main(String[] args) { 5 // Arrays within a HashMap 6 HashMap<String, int[]> arrayMap = new HashMap<>(); 7 arrayMap.put("numbers", new int[] { 1, 2, 3 }); 8 arrayMap.put("letters", new int[] { 10, 11, 12 }); 9 10 // Accessing the second number in the 'letters' array in arrayMap 11 System.out.println(arrayMap.get("letters")[1]); // Output: 11 12 } 13}

This example demonstrates accessing a value from an array that is itself a value in a HashMap.

Error Handling in Nested Data Structures

While retrieving data from nested HashMaps or arrays, it's important to handle potential errors gracefully. This can be done using conditional checks or try-catch blocks.

Error Handling with Nested HashMaps and Arrays:

For nested HashMaps and arrays, check the existence of keys and valid indices.

Java
1import java.util.HashMap; 2 3public class Solution { 4 public static void main(String[] args) { 5 // HashMap within a HashMap 6 HashMap<String, HashMap<String, String>> nestedMap = new HashMap<>(); 7 nestedMap.put("fruit", new HashMap<>() {{ 8 put("apple", "red"); 9 put("banana", "yellow"); 10 }}); 11 nestedMap.put("vegetable", new HashMap<>() {{ 12 put("carrot", "orange"); 13 put("spinach", "green"); 14 }}); 15 16 // Safely accessing the color of apple 17 if (nestedMap.containsKey("fruit") && nestedMap.get("fruit").containsKey("apple")) { 18 System.out.println(nestedMap.get("fruit").get("apple")); // Output: red 19 } else { 20 System.err.println("The key 'apple' or 'fruit' does not exist."); 21 } 22 23 // Arrays within a HashMap 24 HashMap<String, int[]> arrayMap = new HashMap<>(); 25 arrayMap.put("numbers", new int[] { 1, 2, 3 }); 26 arrayMap.put("letters", new int[] { 10, 11, 12 }); 27 28 // Safely accessing the second letter in the 'letters' array 29 if (arrayMap.containsKey("letters") && arrayMap.get("letters").length > 1) { 30 System.out.println(arrayMap.get("letters")[1]); // Output: 11 31 } else { 32 System.err.println("The key 'letters' or index is out of bounds."); 33 } 34 } 35}

By incorporating these conditional checks before accessing the data, we avoid runtime exceptions that could disrupt program execution.

Using try-catch for Error Handling:

Alternatively, you can use try-catch blocks to handle errors.

Java
1import java.util.HashMap; 2 3public class Solution { 4 public static void main(String[] args) { 5 // HashMap within a HashMap 6 HashMap<String, HashMap<String, String>> nestedMap = new HashMap<>(); 7 nestedMap.put("fruit", new HashMap<>() {{ 8 put("apple", "red"); 9 put("banana", "yellow"); 10 }}); 11 nestedMap.put("vegetable", new HashMap<>() {{ 12 put("carrot", "orange"); 13 put("spinach", "green"); 14 }}); 15 16 // Using try-catch to access the color of apple safely 17 try { 18 System.out.println(nestedMap.get("fruit").get("apple")); // Output: red 19 } catch (Exception ex) { 20 System.err.println("Error accessing nested key: " + ex.getMessage()); 21 } 22 23 // Arrays within a HashMap 24 HashMap<String, int[]> arrayMap = new HashMap<>(); 25 arrayMap.put("numbers", new int[] { 1, 2, 3 }); 26 arrayMap.put("letters", new int[] { 10, 11, 12 }); 27 28 // Using try-catch to access the second letter in the 'letters' array safely 29 try { 30 System.out.println(arrayMap.get("letters")[1]); // Output: 11 31 } catch (Exception ex) { 32 System.err.println("Error accessing HashMap with array: " + ex.getMessage()); 33 } 34 } 35}

Using try-catch blocks, we can catch and handle exceptions for more robust error management.

Common Operations on These Structures

The modification of nested arrays and HashMaps is similar to that of their non-nested versions.

Java
1import java.util.HashMap; 2import java.util.Arrays; 3 4public class Solution { 5 public static void main(String[] args) { 6 // HashMap within a HashMap 7 HashMap<String, HashMap<String, String>> nestedMap = new HashMap<>(); 8 nestedMap.put("fruit", new HashMap<>() {{ 9 put("apple", "red"); 10 put("banana", "yellow"); 11 }}); 12 nestedMap.put("vegetable", new HashMap<>() {{ 13 put("carrot", "orange"); 14 put("spinach", "green"); 15 }}); 16 17 // Modifying spinach's color to red 18 nestedMap.get("vegetable").put("spinach", "red"); 19 20 // Print the vegetable HashMap to show modification 21 System.out.println("Vegetable colors after modification: " + nestedMap.get("vegetable")); 22 23 // Arrays within a nested array 24 int[][] nestedArray = { 25 { 1, 2, 3 }, 26 { 4, 5, 6 }, 27 { 7, 8, 9 } 28 }; 29 30 // Deleting the 2nd value from the 3rd array in nested array 31 int[] tempArray = new int[nestedArray[2].length - 1]; 32 for (int i = 0, j = 0; i < nestedArray[2].length; i++) { 33 if (i != 1) { 34 tempArray[j++] = nestedArray[2][i]; 35 } 36 } 37 nestedArray[2] = tempArray; 38 39 // Print the third array to show the deletion 40 System.out.println("Third array after deletion: " + Arrays.toString(nestedArray[2])); 41 42 // Deleting apple from the 'fruit' HashMap in nestedMap 43 nestedMap.get("fruit").remove("apple"); 44 45 // Print the fruit HashMap to show deletion 46 System.out.println("Fruit colors after deletion: " + nestedMap.get("fruit")); 47 } 48}

In this block, we demonstrate various operations, including modifying existing values, adding new elements, and removing elements from nested data structures.

Lesson Summary

Congratulations! You've journeyed through nested arrays and HashMaps, terms that are becoming increasingly common in the data-intensive programming world. We've learned how to create, access, and modify values in these complex structures. Up next, we have hands-on practice sessions to solidify your understanding of these concepts. Get ready to apply what you've learned!

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