帮我优化一下以下这段java代码 private boolean vaildRepeatData(RmMixModelAcceptDataDto rmMixModelAcceptDataDto) { AtomicBoolean result = new AtomicBoolean(false); Integer count = rmMixModelBaseDataDao.selectByAllInfo(rmMixModelAcceptDataDto,DateUtils.getMonth(new Date())); if (count > 0) { result.set(true); } List<RmMixModelAcceptDataDto.Contact> contactList = rmMixModelAcceptDataDto.getContactList(); if (CollectionUtil.isNotEmpty(contactList)) { List<RmMixModelRelationData> rmMixModelRelationDataLocalList = rmMixModelBaseRelationDataDao.selectRelationInfoByApplyNo(rmMixModelAcceptDataDto.getApplNo()); List<RmMixModelRelationData> rmMixModelRelationDataRmList = contactList.stream().map( contact -> { RmMixModelRelationData rmMixModelRelationData = new RmMixModelRelationData(); BeanUtils.copyProperties(contact, rmMixModelRelationData); rmMixModelRelationData.setApplNo(rmMixModelAcceptDataDto.getApplNo()); rmMixModelRelationData.setPMonth(DateUtils.getMonth(new Date())); String contactInfo = Constants.BLANK; try { contactInfo =cryptoConfiguration.hash(rmMixModelRelationData.getContactInfo()); } catch (SQLException e) { log.error("手机号hash加密失败! e:{}",e.getMessage()); } rmMixModelRelationData.setContactInfo(contactInfo); return rmMixModelRelationData; } ).collect(Collectors.toList()); if (rmMixModelRelationDataLocalList.size() != rmMixModelRelationDataRmList.size()){ result.set(false); }else { rmMixModelRelationDataLocalList.forEach( local ->{ for (RmMixModelRelation
时间: 2024-02-14 11:10:03 浏览: 74
(新手必看)优化java代码的29种方法
3星 · 编辑精心推荐
以下是对该 Java 代码的优化建议:
1. 命名优化:Java 方法名一般采用小驼峰命名方式,建议将方法名 `vaildRepeatData` 改为 `isValidRepeatData`。
2. 变量类型优化:`result` 变量可以改为 `boolean` 类型,避免使用 `AtomicBoolean` 类型,因为在此处使用 `AtomicBoolean` 类型的原因不明确。
3. 空值判断优化:在判断 `rmMixModelAcceptDataDto` 中的 `contactList` 是否为空时,可以使用 `Objects.isNull()` 方法代替 `CollectionUtil.isNotEmpty()` 方法。
4. 流式处理优化:将 `rmMixModelRelationDataRmList` 的创建过程使用流式处理方式简化代码。
5. 循环优化:将 `rmMixModelRelationDataLocalList` 和 `rmMixModelRelationDataRmList` 的比较过程使用 `Stream` 流式处理方式简化循环过程,并使用 `allMatch` 方法判断两个集合是否相等。
优化后的代码如下所示:
```java
private boolean isValidRepeatData(RmMixModelAcceptDataDto rmMixModelAcceptDataDto) {
boolean result = false;
Integer count = rmMixModelBaseDataDao.selectByAllInfo(rmMixModelAcceptDataDto, DateUtils.getMonth(new Date()));
if (count > 0) {
result = true;
}
List<RmMixModelRelationData.Contact> contactList = rmMixModelAcceptDataDto.getContactList();
if (Objects.isNull(contactList)) {
return result;
}
List<RmMixModelRelationData> rmMixModelRelationDataRmList = contactList.stream().map(contact -> {
RmMixModelRelationData rmMixModelRelationData = new RmMixModelRelationData();
BeanUtils.copyProperties(contact, rmMixModelRelationData);
rmMixModelRelationData.setApplNo(rmMixModelAcceptDataDto.getApplNo());
rmMixModelRelationData.setPMonth(DateUtils.getMonth(new Date()));
String contactInfo = Constants.BLANK;
try {
contactInfo = cryptoConfiguration.hash(rmMixModelRelationData.getContactInfo());
} catch (SQLException e) {
log.error("手机号 hash 加密失败! e:{}", e.getMessage());
}
rmMixModelRelationData.setContactInfo(contactInfo);
return rmMixModelRelationData;
}).collect(Collectors.toList());
List<RmMixModelRelationData> rmMixModelRelationDataLocalList =
rmMixModelBaseRelationDataDao.selectRelationInfoByApplyNo(rmMixModelAcceptDataDto.getApplNo());
result = rmMixModelRelationDataLocalList.size() == rmMixModelRelationDataRmList.size() &&
IntStream.range(0, rmMixModelRelationDataLocalList.size())
.allMatch(i -> rmMixModelRelationDataLocalList.get(i).equals(rmMixModelRelationDataRmList.get(i)));
return result;
}
```
通过以上优化措施,可以使代码更加简洁、易读,并提高代码的执行效率。
阅读全文