优化这段java代码 //通过域账号名称 调用userSdk获取 对应ID List<String> sendUserNameList = form.getOperationScoreDTOList() .stream().map(OperationScoreDTO::getSenderName).distinct().collect(Collectors.toList()); List<String> receiverUserNameList = form.getOperationScoreDTOList() .stream().map(OperationScoreDTO::getReceiverName).distinct().collect(Collectors.toList()); List<FindOneByUsernameVo> sendUserList = userServiceSdk.findByUsernames(sendUserNameList); List<FindOneByUsernameVo> receiverUserList = userServiceSdk.findByUsernames(receiverUserNameList); Map<String, Integer> senderMap = sendUserList .stream() .collect(Collectors.toMap(FindOneByUsernameVo::getUsername, FindOneByUsernameVo::getTeclibUserId)); Map<String, Integer> receiverMap = receiverUserList .stream() .collect(Collectors.toMap(FindOneByUsernameVo::getUsername, FindOneByUsernameVo::getTeclibUserId)); for (OperationScoreDTO operationScoreDTO : form.getOperationScoreDTOList()) { Integer senderId = senderMap.get(operationScoreDTO.getSenderName()); if (senderId == null) { throw new BizException("获取发送者失败"); } Integer receiverId = receiverMap.get(operationScoreDTO.getSenderName()); if (receiverId == null) { throw new BizException("获取接收者失败"); } Short score = operationScoreDTO.getScore(); String remark = operationScoreDTO.getRemark(); OperationScoreEntity os = new OperationScoreEntity(); os.setSendId(senderId); os.setReceiverId(receiverId); os.setEvent(score.toString()); os.setRemark(remark); os.setSendTime(LocalDate.now()); operationScoreRepository.save(os); String event = ScoreEventConstant.BONUS_SCORE; String module = ScoreModuleConstant.BONUS_SCORE; String payload = String.format("osId:%d;receiver:%d;remark:%s;score:%s", os.getId(), receiverId, remark, score); UserScoreEntity userScore = new UserScoreEntity(); userScore.setUserId(receiverId); userScore.setModule(module); userScore.setEvent(event); userScore.setPayload(payload); userScore.setOperationTime(operationScoreDTO.getOperationTime()); userScore.setScore(score); Integer totalScore = this.findTotalScoreByUserId(receiverId); userScore.setTotal(Math.max(totalScore + score, 0)); userScoreRepository.save(userScore); } return Boolean.TRUE;
时间: 2024-04-15 10:30:57 浏览: 128
(新手必看)优化java代码的29种方法
3星 · 编辑精心推荐
在这段代码中,你可以考虑以下优化措施:
1. 将重复的代码提取为方法:将获取用户名列表和调用 `userServiceSdk.findByUsernames` 的逻辑提取为一个方法,这样可以减少重复代码。
2. 使用 `forEach` 替代 `for` 循环:使用流的 `forEach` 方法可以简化循环的书写,更加简洁和直观。
3. 使用批量插入:使用批量插入操作可以减少数据库交互次数,提高性能。
下面是优化后的代码示例:
```java
List<String> sendUserNameList = form.getOperationScoreDTOList()
.stream().map(OperationScoreDTO::getSenderName).distinct().collect(Collectors.toList());
List<String> receiverUserNameList = form.getOperationScoreDTOList()
.stream().map(OperationScoreDTO::getReceiverName).distinct().collect(Collectors.toList());
Map<String, Integer> senderMap = getUserIdsByUsernames(sendUserNameList);
Map<String, Integer> receiverMap = getUserIdsByUsernames(receiverUserNameList);
form.getOperationScoreDTOList().forEach(operationScoreDTO -> {
Integer senderId = senderMap.get(operationScoreDTO.getSenderName());
if (senderId == null) {
throw new BizException("获取发送者失败");
}
Integer receiverId = receiverMap.get(operationScoreDTO.getSenderName());
if (receiverId == null) {
throw new BizException("获取接收者失败");
}
Short score = operationScoreDTO.getScore();
String remark = operationScoreDTO.getRemark();
OperationScoreEntity os = new OperationScoreEntity();
os.setSendId(senderId);
os.setReceiverId(receiverId);
os.setEvent(score.toString());
os.setRemark(remark);
os.setSendTime(LocalDate.now());
operationScoreRepository.save(os);
String event = ScoreEventConstant.BONUS_SCORE;
String module = ScoreModuleConstant.BONUS_SCORE;
String payload = String.format("osId:%d;receiver:%d;remark:%s;score:%s",
os.getId(), receiverId, remark, score);
UserScoreEntity userScore = new UserScoreEntity();
userScore.setUserId(receiverId);
userScore.setModule(module);
userScore.setEvent(event);
userScore.setPayload(payload);
userScore.setOperationTime(operationScoreDTO.getOperationTime());
userScore.setScore(score);
Integer totalScore = this.findTotalScoreByUserId(receiverId);
userScore.setTotal(Math.max(totalScore + score, 0));
userScoreRepository.save(userScore);
});
return Boolean.TRUE;
private Map<String, Integer> getUserIdsByUsernames(List<String> usernames) {
List<FindOneByUsernameVo> userList = userServiceSdk.findByUsernames(usernames);
return userList.stream()
.collect(Collectors.toMap(FindOneByUsernameVo::getUsername, FindOneByUsernameVo::getTeclibUserId));
}
```
通过上述优化,你可以减少代码重复,提高代码的可读性和维护性,并且减少数据库交互次数,提高性能。
阅读全文