public List<InsuranceEnumVO> paymentPlanInsurance(){ return Arrays.stream(SocialSecurityInsuranceEnum.values()) .map(InsuranceEnumVO::new) .collect(Collectors.toList());}InsuranceEnumVO 类中添加一个构造函数,参数为 SocialSecurityInsuranceEnum 类型的枚举值
时间: 2024-03-05 14:48:42 浏览: 10
可以在 InsuranceEnumVO 类中添加如下构造函数:
```
public InsuranceEnumVO(SocialSecurityInsuranceEnum insuranceEnum) {
this.code = insuranceEnum.getCode();
this.name = insuranceEnum.getName();
}
```
这个构造函数的作用是将 SocialSecurityInsuranceEnum 类型的枚举值转化为 InsuranceEnumVO 类型的对象,其中 code 和 name 分别来自于 SocialSecurityInsuranceEnum 枚举值的 getCode() 和 getName() 方法。这样,在调用 paymentPlanInsurance() 方法时,就可以将 SocialSecurityInsuranceEnum 枚举值转化为 InsuranceEnumVO 对象,方便进行后续的操作。
相关问题
优化以下代码 public List<InsuranceEnumVO> paymentPlanInsurance(){ List<InsuranceEnumVO> insuranceEnums = new ArrayList<>(); SocialSecurityInsuranceEnum[] enums = SocialSecurityInsuranceEnum.values(); for (SocialSecurityInsuranceEnum socialSecurityInsuranceEnum : enums) { InsuranceEnumVO insurance = new InsuranceEnumVO(); insurance.setId(socialSecurityInsuranceEnum.getId()); insurance.setName(socialSecurityInsuranceEnum.getValue()); insuranceEnums.add(insurance); } return insuranceEnums; }
可以将循环改为使用 Java 8 的 Stream API,使用 lambda 表达式和方法引用来简化代码,从而实现更好的可读性和性能:
```
public List<InsuranceEnumVO> paymentPlanInsurance(){
return Arrays.stream(SocialSecurityInsuranceEnum.values())
.map(InsuranceEnumVO::new)
.collect(Collectors.toList());
}
```
其中,`map` 方法将 `SocialSecurityInsuranceEnum` 转换为 `InsuranceEnumVO`,`collect` 方法将结果收集到一个列表中。同时,需要在 `InsuranceEnumVO` 类中添加一个构造函数,参数为 `SocialSecurityInsuranceEnum` 类型的枚举值,以便将其转换为 `InsuranceEnumVO` 对象。
public List<String> bxGetExcelList(List<LightGroupReq> lightGroupReqs) { LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X); ArrayList<String> writeBinList = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 78; i++) { frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); int[] temp = new int[79]; //全部初始化为-1 Arrays.fill(temp, -1); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } // 补充FF 数据帧 for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); portMap.clear(); if(timeStamp>7){ timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { BinaryFileUtils.bxFramesToHex(framesList.get(j),timeStamp,writeBinList); } timeStamp++; } return writeBinList; }优化这段代码
1. 在for循环中创建的Frames对象可以提取出来,避免重复创建对象,提高效率。
2. 可以将填充-1的数组temp改为使用HashSet或LinkedHashSet,避免重复遍历数组。
3. 可以使用Lambda表达式代替for循环中的操作,简化代码。
4. 可以使用Map的computeIfAbsent方法避免重复判断key是否存在。
重构后的代码如下:
```
public List<String> bxGetExcelList(List<LightGroupReq> lightGroupReqs) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X);
ArrayList<String> writeBinList = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
for (int i = 1; i <= 78; i++) {
frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
groupingFrames.put((long) groupingFrames.size(), frameList);
long timeStamp = 0;
for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) {
List<Frames> framesList = entry.getValue();
Set<Integer> groupIndexSet = new HashSet<>();
framesList.forEach(f -> groupIndexSet.add(f.getGroupIndex()));
// 补充FF 数据帧
groupIndexSet.stream()
.filter(i -> !framesList.stream().anyMatch(f -> f.getGroupIndex() == i))
.forEach(i -> portMap.computeIfAbsent(i, k -> new Frames(7, 63, 63, 2550, 15, 15, 0, k, 0, 2)));
framesList.addAll(portMap.values());
framesList.sort(Comparator.comparing(Frames::getGroupIndex));
portMap.clear();
if (timeStamp > 7) {
timeStamp = 0;
}
framesList.forEach(f -> BinaryFileUtils.bxFramesToHex(f, timeStamp, writeBinList));
timeStamp++;
}
return writeBinList;
}
```