优化这段代码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);
时间: 2023-05-12 14:04:01 浏览: 146
优化代码
这段代码可以优化为:
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);
这样可以减少代码行数,提高代码的可读性和可维护性。
阅读全文