优化一下代码 JSONObject typeObj = new JSONObject(); Map<String, Integer> typeMap = new HashMap<>(); typeMap.put(SpecialWorkType.FIRE_WORK.desc, 0); typeMap.put(SpecialWorkType.CONFINED_WORK.desc, 0); typeMap.put(SpecialWorkType.MAINTENANCE_WORK.desc, 0); typeMap.put(SpecialWorkType.HIGH_WORK.desc, 0); typeMap.put(SpecialWorkType.LIFTING_WORK.desc, 0); typeMap.put(SpecialWorkType.ELECTRICIAN_WORK.desc, 0); typeMap.put(SpecialWorkType.LAND_WORK.desc, 0); typeMap.put(SpecialWorkType.ROAD_WORK.desc, 0); specialOperations.forEach(v -> { Integer domainCount = domainMap.get(v.getDomainId()); if (domainCount == null) { domainMap.put(v.getDomainId(), 1); } else { domainMap.put(v.getDomainId(), ++domainCount); } Integer typeCount = typeMap.get(v.getJobType().desc); typeMap.put(v.getJobType().desc, ++typeCount); });
时间: 2024-03-20 15:44:42 浏览: 87
可以尝试使用更简洁的方式来优化这段代码,如下所示:
```
Map<Long, Integer> domainMap = specialOperations.stream()
.collect(Collectors.groupingBy(SpecialOperation::getDomainId, Collectors.summingInt(e -> 1)));
Map<String, Integer> typeMap = specialOperations.stream()
.collect(Collectors.groupingBy(v -> v.getJobType().desc, Collectors.summingInt(e -> 1)));
```
这里使用了Java 8引入的Stream API,利用 `Collectors.groupingBy()` 方法将特定属性相同的对象进行分组,并使用 `Collectors.summingInt()` 方法进行计数。这样可以简化代码,并提高代码的可读性和可维护性。
相关问题
Map<String, Object> map = new HashMap<String, Object>(); map.put("createdateGt",tbOrderInfo.getCreatedateGt()); map.put("createdateLt",tbOrderInfo.getCreatedateLt()); map.put("saleOrderId",tbOrderInfo.getSaleOrderId()); map.put("hm",tbOrderInfo.getHm()); map.put("orderDetail",tbOrderInfo.getOrderDetail()); map.put("cityCode",tbOrderInfo.getCityCode()); map.put("status",tbOrderInfo.getStatus()); map.put("areaCode",tbOrderInfo.getAreaCode()); map.put("orderType",tbOrderInfo.getOrdertype()); page= tbOrderInfoService.findByPage(map, page); for (TbOrderInfo orderInfo1 : page.getItems()) { List<Map<String, Object>> usermap = tbUserInfoService.findCustom("select * from tb_User_Info where (username = #{p[0]} or phone =#{p[0]}) and isDelete = 0", new Object[]{orderInfo1.getDistribId()}); if (!usermap.isEmpty()) { orderInfo1.setJson(new JSONObject().fluentPut("area2", usermap.get(0).get("AREA2"))); } } for (TbOrderInfo orderInfo1 : page.getItems()) { List<Map<String, Object>> usermap = tbUserInfoService.findCustom("select * from tb_User_Info where (username = #{p[0]} or phone =#{p[0]}) and isDelete = 0", new Object[]{orderInfo1.getDistribId()}); if (!usermap.isEmpty()) { orderInfo1.setJson(new JSONObject().fluentPut("level", usermap.get(0).get("level"))); } }怎么改,不要findCustom并且改为list
要将代码中的 `findCustom` 方法替换为使用 `List`,你可以使用 `JdbcTemplate` 类提供的 `queryForList` 方法来执行 SQL 查询并返回结果列表。下面是修改后的代码示例:
```java
Map<String, Object> map = new HashMap<String, Object>();
map.put("createdateGt", tbOrderInfo.getCreatedateGt());
map.put("createdateLt", tbOrderInfo.getCreatedateLt());
map.put("saleOrderId", tbOrderInfo.getSaleOrderId());
map.put("hm", tbOrderInfo.getHm());
map.put("orderDetail", tbOrderInfo.getOrderDetail());
map.put("cityCode", tbOrderInfo.getCityCode());
map.put("status", tbOrderInfo.getStatus());
map.put("areaCode", tbOrderInfo.getAreaCode());
map.put("orderType", tbOrderInfo.getOrdertype());
page = tbOrderInfoService.findByPage(map, page);
for (TbOrderInfo orderInfo1 : page.getItems()) {
List<Map<String, Object>> usermap = jdbcTemplate.queryForList("select * from tb_User_Info where (username = ? or phone = ?) and isDelete = 0", orderInfo1.getDistribId(), orderInfo1.getDistribId());
if (!usermap.isEmpty()) {
orderInfo1.setJson(new JSONObject().fluentPut("area2", usermap.get(0).get("AREA2")));
}
}
for (TbOrderInfo orderInfo1 : page.getItems()) {
List<Map<String, Object>> usermap = jdbcTemplate.queryForList("select * from tb_User_Info where (username = ? or phone = ?) and isDelete = 0", orderInfo1.getDistribId(), orderInfo1.getDistribId());
if (!usermap.isEmpty()) {
orderInfo1.setJson(new JSONObject().fluentPut("level", usermap.get(0).get("level")));
}
}
```
在上述修改后的代码中,我们使用 `jdbcTemplate.queryForList` 方法执行 SQL 查询,并将结果存储在 `List<Map<String, Object>>` 中。注意,我们使用 `?` 占位符来代替参数值,并在方法调用中传递参数值。这样可以保证查询语句的安全性,并避免 SQL 注入攻击的风险。
public class ManagerLogin extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置头文件 response.setContentType("application/json; charset=utf8"); PrintWriter out = response.getWriter(); // 获取账号密码 String user = request.getParameter("user"); String psw = request.getParameter("psw"); // 设置响应map HashMap<String, Object> hashMap = new HashMap<String, Object>(); Manager manager = new Manager(); String result = null; try { result = manager.login(user, psw); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } if (result.equals("1")) { HttpSession session = request.getSession(); session.setAttribute("manager", user); session.setAttribute("manager_first", "1"); // 登录 hashMap.put("code", 0); hashMap.put("msg", "登录成功"); hashMap.put("url", request.getContextPath() + "/manager/01nav.jsp"); } else { hashMap.put("code", 1); hashMap.put("msg", result); } // response.sendRedirect(request.getContextPath() +"/test.jsp"); JSONObject json = JSONObject.fromObject(hashMap); out.write(json.toString()); }
这段代码是一个Java Servlet,处理了前端通过Ajax POST请求发送的登录数据。在doPost方法中,首先设置了响应头部的Content-Type为application/json; charset=utf8,并获取了请求中的账号和密码。接着,创建了一个HashMap对象,用于存储响应数据。然后,调用Manager类的login方法,返回一个结果字符串,如果结果字符串为"1",表示登录成功,将登录状态保存在session中,并将code设置为0,msg设置为"登录成功",url设置为"/manager/01nav.jsp"。否则,将code设置为1,msg设置为结果字符串。最后,将HashMap对象转换成JSON格式,通过PrintWriter将响应数据返回给前端。
阅读全文