求助:如何将Go语言Map转换为指定结构的目标Map
Map结构转换解决方案
需求说明
需要将给定的输入Map转换为指定结构的输出Map,核心是将*_brand中的品牌名称与*_model中对应品牌前缀的键值对关联,重组为以品牌为键、包含对应型号值的结构。尝试过switch/case、循环遍历等方式未成功,需明确键值匹配逻辑。
输入示例
Values{ "toto_voiture_brand": Ad{ "CITROEN": "CITROEN", }, "toto_voiture_model": Ad{ "CITROEN_toto": "C3", }, "toto_moto_brand": Ad{ "KAWASAKI": "KAWASAKI", }, "toto_moto_model": Ad{ "KAWASAKI_tata": "Ninja 1000 SX", }, "toto_camion_brand": Ad{ "RENAULT": "RENAULT", "PEUGEOT": "PEUGEOT", }, "toto_camion_model": Ad{ "RENAULT_toto": "J5", "PEUGEOT_tata": "255", }, }
输出示例
Values{ "toto_voiture_model": { "Citroen": { {Value: "C3"}, }, }, "toto_moto_model": { "Kawasaki": { {Value: "Ninja 1000 SX"}, }, }, "toto_camion_model": { "RENAULT": { {Value: "J5"}, }, "PEUGEOT": { {Value: "255"}, }, }, }
解决方案(Java示例)
核心思路是分组匹配品牌与型号的关联关系,无需复杂的switch/case,通过字符串匹配和循环遍历即可实现:
- 从输入Map中筛选所有
*_brand和*_model键值对,按前缀(如toto_voiture)分组; - 遍历每组的品牌Map,获取每个品牌名称;
- 在对应型号Map中,找到以该品牌名称为前缀的键,提取型号值;
- 构建目标结构,将品牌作为键,型号值封装后存入对应
*_model的结果Map。
具体代码实现:
import java.util.HashMap; import java.util.Map; public class MapTransformer { public static Map<String, Map<String, Map<String, Object>>> transform(Map<String, Map<String, String>> input) { Map<String, Map<String, Map<String, Object>>> output = new HashMap<>(); // 遍历所有品牌相关的键 for (Map.Entry<String, Map<String, String>> entry : input.entrySet()) { String key = entry.getKey(); if (!key.endsWith("_brand")) continue; // 匹配对应的型号键 String modelKey = key.replace("_brand", "_model"); Map<String, String> brandMap = entry.getValue(); Map<String, String> modelMap = input.get(modelKey); if (modelMap == null) continue; // 初始化当前型号对应的结果容器 Map<String, Map<String, Object>> brandResult = new HashMap<>(); output.put(modelKey, brandResult); // 遍历每个品牌,关联对应型号 for (String brand : brandMap.values()) { for (Map.Entry<String, String> modelEntry : modelMap.entrySet()) { if (modelEntry.getKey().startsWith(brand + "_")) { // 封装指定结构 Map<String, Object> valueMap = new HashMap<>(); valueMap.put("Value", modelEntry.getValue()); // 处理品牌名称首字母小写,不需要可直接用brand String formattedBrand = brand.substring(0, 1) + brand.substring(1).toLowerCase(); brandResult.put(formattedBrand, valueMap); break; // 每个品牌对应一个型号,找到即退出内层循环 } } } } return output; } public static void main(String[] args) { // 模拟输入数据 Map<String, Map<String, String>> input = new HashMap<>(); // 组装toto_voiture数据 Map<String, String> voitureBrand = new HashMap<>(); voitureBrand.put("CITROEN", "CITROEN"); input.put("toto_voiture_brand", voitureBrand); Map<String, String> voitureModel = new HashMap<>(); voitureModel.put("CITROEN_toto", "C3"); input.put("toto_voiture_model", voitureModel); // 组装toto_moto数据 Map<String, String> motoBrand = new HashMap<>(); motoBrand.put("KAWASAKI", "KAWASAKI"); input.put("toto_moto_brand", motoBrand); Map<String, String> motoModel = new HashMap<>(); motoModel.put("KAWASAKI_tata", "Ninja 1000 SX"); input.put("toto_moto_model", motoModel); // 组装toto_camion数据 Map<String, String> camionBrand = new HashMap<>(); camionBrand.put("RENAULT", "RENAULT"); camionBrand.put("PEUGEOT", "PEUGEOT"); input.put("toto_camion_brand", camionBrand); Map<String, String> camionModel = new HashMap<>(); camionModel.put("RENAULT_toto", "J5"); camionModel.put("PEUGEOT_tata", "255"); input.put("toto_camion_model", camionModel); // 执行转换并打印结果 Map<String, Map<String, Map<String, Object>>> result = transform(input); System.out.println("Values{"); for (Map.Entry<String, Map<String, Map<String, Object>>> outerEntry : result.entrySet()) { System.out.println(" \"" + outerEntry.getKey() + "\": {"); for (Map.Entry<String, Map<String, Object>> middleEntry : outerEntry.getValue().entrySet()) { System.out.println(" \"" + middleEntry.getKey() + "\": {"); System.out.println(" {Value: \"" + middleEntry.getValue().get("Value") + "\"},"); System.out.println(" },"); } System.out.println(" },"); } System.out.println("}"); } }
关键逻辑说明
- 键匹配:通过
replace("_brand", "_model")动态找到对应型号Map,避免硬编码前缀; - 品牌型号关联:用
startsWith(brand + "_")精准匹配型号Map中对应品牌的键; - 结构封装:将型号值存入
{Value: ...}结构,同时支持品牌名称大小写转换,不需要可直接使用原字符串。
内容的提问来源于stack exchange,提问作者Mona
相关产品推荐
相关产品推荐

