如何将String对象存入ArrayList并改造ArrayList<HashMap<String,String>>元素结构?
Hey there! Let's tackle your two Java collection questions one by one.
First off, let's clarify what you're aiming for here—you either want to replace the HashMap elements in your original list with Strings, or create a new ArrayList for Strings while clearing out the original HashMap list. Let's cover both scenarios:
Scenario 1: Create a new String list and clear the original HashMap list
This is the cleaner approach since mixing HashMap and String types in the same list isn't ideal (it breaks type safety). Here's how to do it:
// Your original HashMap list ArrayList<HashMap<String, String>> statusList = new ArrayList<>(); // Assume this list is already populated with your HashMap entries // New list to hold your Strings ArrayList<String> stringList = new ArrayList<>(); // Extract the String value you need from each HashMap (e.g., status_name) for (HashMap<String, String> map : statusList) { // Customize this to get the String you want—here we're using the status_name value String targetString = "status_name=" + map.get("status_name"); stringList.add(targetString); } // Remove all HashMap elements from the original list statusList.clear();
Scenario 2: Replace elements in the original list (not recommended for type safety)
If you really need to modify the original list directly, note that you'll lose generic type safety (the list will become ArrayList<Object>). Example:
ArrayList<HashMap<String, String>> originalList = new ArrayList<>(); // Populate the list first for (int i = 0; i < originalList.size(); ) { // Remove the HashMap at index i HashMap<String, String> map = originalList.remove(i); // Get your target String String value = map.get("status_name"); // Add the String back at the same index originalList.add(i, value); i++; }
Your input list is [{status_id=1, status_name=iceCream}, {status_id=2, status_name=cake (chocolate)}], and you want an output like [status_name=iceCream, status_name=cake(chocolate)]. It looks like you want a list of Strings in that key-value format—here's how to make it happen:
// Initialize your input list ArrayList<HashMap<String, String>> statusList = new ArrayList<>(); HashMap<String, String> firstMap = new HashMap<>(); firstMap.put("status_id", "1"); firstMap.put("status_name", "iceCream"); HashMap<String, String> secondMap = new HashMap<>(); secondMap.put("status_id", "2"); secondMap.put("status_name", "cake (chocolate)"); statusList.add(firstMap); statusList.add(secondMap); // Create the result list ArrayList<String> resultList = new ArrayList<>(); for (HashMap<String, String> map : statusList) { // Remove the status_id entry from the original map (if you need to modify it) map.remove("status_id"); // Build the string in your desired format—remove spaces if needed String statusEntry = "status_name=" + map.get("status_name").replace(" ", ""); resultList.add(statusEntry); } // Print the result—this will match your expected output System.out.println(resultList); // Output: [status_name=iceCream, status_name=cake(chocolate)]
If instead you wanted a list of HashMaps that only contain the status_name key (instead of Strings), you can adjust the code like this:
ArrayList<HashMap<String, String>> resultList = new ArrayList<>(); for (HashMap<String, String> map : statusList) { HashMap<String, String> trimmedMap = new HashMap<>(); trimmedMap.put("status_name", map.get("status_name").replace(" ", "")); resultList.add(trimmedMap); } // Output will be: [{status_name=iceCream}, {status_name=cake(chocolate)}]
内容的提问来源于stack exchange,提问作者Ashley

