List<Map<String,Object>> resList = new ArrayList<>(); Map<String,Object> PlanMap =new HashMap<>(); PlanMap.put("area_name",resMap.get("area_name")); PlanMap.put("type",resMap.get("type")); PlanMap.put("child_num","一孩"); PlanMap.put("men",resMap.get("one_child_boys")); PlanMap.put("women",resMap.get("one_child_girls")); resList.add(PlanMap); PlanMap =new HashMap<>(); PlanMap.put("area_name",resMap.get("area_name")); PlanMap.put("type",resMap.get("type")); PlanMap.put("child_num","二孩"); PlanMap.put("men",resMap.get("two_child_boys")); PlanMap.put("women",resMap.get("two_child_girls")); resList.add(PlanMap); PlanMap =new HashMap<>(); PlanMap.put("area_name",resMap.get("area_name")); PlanMap.put("type",resMap.get("type")); PlanMap.put("child_num","三孩"); PlanMap.put("men",resMap.get("three_child_boys")); PlanMap.put("women",resMap.get("three_child_girls")); resList.add(PlanMap); PlanMap =new HashMap<>(); PlanMap.put("area_name",resMap.get("area_name")); PlanMap.put("type",resMap.get("type")); PlanMap.put("child_num","四孩及以上"); PlanMap.put("men",resMap.get("many_child_boys")); PlanMap.put("women",resMap.get("many_child_girls")); resList.add(PlanMap);
时间: 2023-05-13 16:06:13 浏览: 90
这段代码是一个 Java 程序,它创建了一个 List 对象 resList,其中包含了多个 Map 对象 PlanMap。每个 PlanMap 对象都包含了一个地区名称、一个类型、一个孩子数量、男孩数量和女孩数量。这段代码的作用是将这些数据存储在 resList 中,以便后续使用。
相关问题
优化这段代码List<Map<String,Object>> resList = new ArrayList<>(); Map<String,Object> map1 =new HashMap<>(); map1.put("area_name",resMap.get("area_name")); map1.put("type",resMap.get("type")); map1.put("child_num","一孩"); map1.put("men",resMap.get("one_child_boys")); map1.put("women",resMap.get("one_child_girls")); Map<String,Object> map2 =new HashMap<>(); map2.put("area_name",resMap.get("area_name")); map2.put("type",resMap.get("type")); map2.put("child_num","二孩"); map2.put("men",resMap.get("two_child_boys")); map2.put("women",resMap.get("two_child_girls")); Map<String,Object> map3 =new HashMap<>(); map3.put("area_name",resMap.get("area_name")); map3.put("type",resMap.get("type")); map3.put("child_num","多孩"); map3.put("men",resMap.get("many_child_boys")); map3.put("women",resMap.get("many_child_girls")); resList.add(map1); resList.add(map2); resList.add(map3);
这段代码可以优化为:
List<Map<String,Object>> resList = new ArrayList<>();
Map<String,Object> map =new HashMap<>();
map.put("area_name",resMap.get("area_name"));
map.put("type",resMap.get("type"));
map.put("child_num","一孩");
map.put("men",resMap.get("one_child_boys"));
map.put("women",resMap.get("one_child_girls"));
resList.add(map);
map =new HashMap<>();
map.put("area_name",resMap.get("area_name"));
map.put("type",resMap.get("type"));
map.put("child_num","二孩");
map.put("men",resMap.get("two_child_boys"));
map.put("women",resMap.get("two_child_girls"));
resList.add(map);
map =new HashMap<>();
map.put("area_name",resMap.get("area_name"));
map.put("type",resMap.get("type"));
map.put("child_num","多孩");
map.put("men",resMap.get("many_child_boys"));
map.put("women",resMap.get("many_child_girls"));
resList.add(map);
这样可以减少代码行数,提高代码的可读性和可维护性。
Map<String,Object> map1 =new HashMap<>(); map1.put("area_name",resMap.get("area_name")); map1.put("type",resMap.get("type")); map1.put("child_num","一孩"); map1.put("men",resMap.get("one_child_boys")); map1.put("women",resMap.get("one_child_girls")); Map<String,Object> map2 =new HashMap<>(); map2.put("area_name",resMap.get("area_name")); map2.put("type",resMap.get("type")); map2.put("child_num","二孩"); map2.put("men",resMap.get("two_child_boys")); map2.put("women",resMap.get("two_child_girls")); Map<String,Object> map3 =new HashMap<>(); map3.put("area_name",resMap.get("area_name")); map3.put("type",resMap.get("type")); map3.put("child_num","多孩"); map3.put("men",resMap.get("many_child_boys")); map3.put("women",resMap.get("many_child_girls")); resList.add(map1); resList.add(map2); resList.add(map3);
这段代码是使用 Java 语言创建了三个 HashMap 对象,分别为 map1、map2 和 map3,每个 HashMap 对象中都存储了一个地区的相关信息,包括地区名称、类型、孩子数量、男孩数量和女孩数量。这三个 HashMap 对象被添加到了一个名为 resList 的 ArrayList 中。
阅读全文