Lesson 4
Parsing and Updating Nested HashMaps in Java
Introduction

Hello, and welcome back! Are you ready for a new challenge? In this unit, we're stepping up a notch to tackle a complex yet intriguing task. It involves parsing complex strings into Java HashMaps and then updating them, which is a common requirement in many real-world tasks. So yes, this unit's session is going to be pretty pragmatic — just the way you like it!

Task Statement

This task involves transforming a given string into a nested HashMap and updating a specific key-value pair within that map. The input string will take the form "Key1=Value1,Key2=Value2,...". When a part of the value is another key-value string, we create a nested HashMap.

For example, the string "A1=B1,C1={D1=E1,F1=G1},I1=J1" should be transformed into the following nested HashMap:

Java
1HashMap<String, HashMap<String, String>> dictionary = new HashMap<String, HashMap<String, String>>() {{ 2 put("A1", new HashMap<String, String>() {{ 3 put("", "B1"); 4 }}); 5 put("C1", new HashMap<String, String>() {{ 6 put("D1", "E1"); 7 put("F1", "G1"); 8 }}); 9 put("I1", new HashMap<String, String>() {{ 10 put("", "J1"); 11 }}); 12}};

Your Java function should parse this string into the above HashMap, then update the value of the nested key F1 from G1 to some other value, say NewValue. The function should ultimately return the updated HashMap.

Step 1 - Setting Up the Function and Variables

First, set up the function and necessary variables:

Java
1import java.util.HashMap; 2 3public class Solution { 4 5 public static HashMap<String, HashMap<String, String>> parseString(String inputString) { 6 HashMap<String, HashMap<String, String>> result = new HashMap<>(); 7 8 String key = ""; // to store the outer map key 9 HashMap<String, String> innerMap = new HashMap<>(); // to store the inner map 10 boolean inInnerMap = false; // flag to check if we are inside an inner map 11 int i = 0; // to iterate through the string
Step 2 - Handling the Opening and Closing Braces

Next, handle the opening and closing braces. If an inner map is encountered, set the flag and prepare to parse it:

Java
1 while (i < inputString.length()) { 2 if (inputString.charAt(i) == '{') { 3 // Entering an inner map 4 inInnerMap = true; 5 i++; // Skip the '{' 6 } else if (inputString.charAt(i) == '}') { 7 // Exiting an inner map 8 result.put(key, innerMap); 9 innerMap = new HashMap<>(); 10 inInnerMap = false; 11 i++; // Skip the '}' 12 if (i < inputString.length() && inputString.charAt(i) == ',') { 13 i++; // Skip the ',' after '}' 14 } 15 }
Step 3 - Parsing Outer Map Key-Value Pairs

Handle parsing key-value pairs in the outer map:

Java
1 else if (!inInnerMap) { 2 // Parsing key-value pairs in the outer map 3 int equalPos = inputString.indexOf('=', i); 4 int commaPos = inputString.indexOf(',', equalPos); 5 if (commaPos == -1) commaPos = inputString.length(); 6 7 key = inputString.substring(i, equalPos); 8 String value = inputString.substring(equalPos + 1, commaPos); 9 10 if (value.contains("{")) { 11 // Value is a nested map, will be processed separately 12 i = equalPos + 1; // Move forward to process the nested part 13 } else { 14 // Value is a simple string, add to result map 15 result.put(key, new HashMap<String, String>() {{ 16 put("", value); 17 }}); 18 i = commaPos + 1; // Move past the comma 19 } 20 }
Step 4 - Parsing Inner Map Key-Value Pairs

Handle parsing key-value pairs inside the inner map:

Java
1 else if (inInnerMap) { 2 // Parsing key-value pairs inside the inner map 3 int equalPos = inputString.indexOf('=', i); 4 int commaPos = inputString.indexOf(',', equalPos); 5 int bracePos = inputString.indexOf('}', equalPos); 6 7 // Determine the next delimiter that ends the current key-value pair 8 int endPos = Math.min(commaPos < bracePos ? commaPos : inputString.length(), bracePos); 9 if (endPos == inputString.length()) endPos = Math.max(commaPos, bracePos); 10 11 String innerKey = inputString.substring(i, equalPos); 12 String innerValue = inputString.substring(equalPos + 1, endPos); 13 innerMap.put(innerKey, innerValue); 14 15 i = endPos; 16 if (i < inputString.length() && inputString.charAt(i) == ',') { 17 i++; // Skip the comma 18 } 19 } 20 } 21 22 return result; 23 }
Step 5 - Updating the Value in the Map

Now that we have the parsed HashMap, we can move into the final phase of the task: updating a specific key-value pair. Here’s the function to update a value in the nested HashMap:

Java
1 public static void updateMap(HashMap<String, HashMap<String, String>> map, String key, String value) { 2 for (HashMap<String, String> innerMap : map.values()) { 3 if (innerMap.containsKey(key)) { 4 // Key found, update the value 5 innerMap.put(key, value); 6 return; 7 } 8 } 9 }
Step 6 - Putting It All Together

Finally, we put everything together in one function to parse the string and update the value:

Java
1import java.util.HashMap; 2 3public class Solution { 4 5 public static HashMap<String, HashMap<String, String>> parseString(String inputString) { 6 HashMap<String, HashMap<String, String>> result = new HashMap<>(); 7 8 String key = ""; // to store the outer map key 9 HashMap<String, String> innerMap = new HashMap<>(); // to store the inner map 10 boolean inInnerMap = false; // flag to check if we are inside an inner map 11 int i = 0; // to iterate through the string 12 while (i < inputString.length()) { 13 if (inputString.charAt(i) == '{') { 14 // Entering an inner map 15 inInnerMap = true; 16 i++; // Skip the '{' 17 } else if (inputString.charAt(i) == '}') { 18 // Exiting an inner map 19 result.put(key, innerMap); 20 innerMap = new HashMap<>(); 21 inInnerMap = false; 22 i++; // Skip the '}' 23 if (i < inputString.length() && inputString.charAt(i) == ',') { 24 i++; // Skip the ',' after '}' 25 } 26 } 27 else if (!inInnerMap) { 28 // Parsing key-value pairs in the outer map 29 int equalPos = inputString.indexOf('=', i); 30 int commaPos = inputString.indexOf(',', equalPos); 31 if (commaPos == -1) commaPos = inputString.length(); 32 33 key = inputString.substring(i, equalPos); 34 String value = inputString.substring(equalPos + 1, commaPos); 35 36 if (value.contains("{")) { 37 // Value is a nested map, will be processed separately 38 i = equalPos + 1; // Move forward to process the nested part 39 } else { 40 // Value is a simple string, add to result map 41 result.put(key, new HashMap<String, String>() {{ 42 put("", value); 43 }}); 44 i = commaPos + 1; // Move past the comma 45 } 46 } 47 else if (inInnerMap) { 48 // Parsing key-value pairs inside the inner map 49 int equalPos = inputString.indexOf('=', i); 50 int commaPos = inputString.indexOf(',', equalPos); 51 int bracePos = inputString.indexOf('}', equalPos); 52 53 // Determine the next delimiter that ends the current key-value pair 54 int endPos = Math.min(commaPos < bracePos ? commaPos : inputString.length(), bracePos); 55 if (endPos == inputString.length()) endPos = Math.max(commaPos, bracePos); 56 57 String innerKey = inputString.substring(i, equalPos); 58 String innerValue = inputString.substring(equalPos + 1, endPos); 59 innerMap.put(innerKey, innerValue); 60 61 i = endPos; 62 if (i < inputString.length() && inputString.charAt(i) == ',') { 63 i++; // Skip the comma 64 } 65 } 66 } 67 68 return result; 69 } 70 public static void updateMap(HashMap<String, HashMap<String, String>> map, String key, String value) { 71 for (HashMap<String, String> innerMap : map.values()) { 72 if (innerMap.containsKey(key)) { 73 // Key found, update the value 74 innerMap.put(key, value); 75 return; 76 } 77 } 78 } 79 public static HashMap<String, HashMap<String, String>> parseStringAndUpdateValue(String inputString, String updateKey, String newValue) { 80 // Parse the input string into a nested HashMap 81 HashMap<String, HashMap<String, String>> map = parseString(inputString); 82 // Update the specific key-value pair 83 updateMap(map, updateKey, newValue); 84 return map; 85 } 86 87 public static void main(String[] args) { 88 String input = "A1=B1,C1={D1=E1,F1=G1},I1=J1"; 89 String updateKey = "F1"; 90 String newValue = "NewValue"; 91 92 HashMap<String, HashMap<String, String>> updatedMap = parseStringAndUpdateValue(input, updateKey, newValue); 93 System.out.println(updatedMap); 94 } 95}
Lesson Summary

Well done! You've completed an intensive hands-on session dealing with complex strings and nested HashMaps in Java. This type of task often mirrors real-life scenarios where you process complex data and make updates based on particular criteria.

Now it's your turn to reinforce what you've learned in this unit. Try practicing with different strings and attempting to update various key-value pairs. With practice, you'll be able to apply these coding strategies to a wide range of problems. Happy coding!

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