public ActionResult selectAll() { List<SettingsPO> settingsPOS = settingsMapper.selectAll(); return new ActionResult().success(settingsPOS); } 改为数组
时间: 2023-12-12 14:02:53 浏览: 30
您可以将返回类型从 `List<SettingsPO>` 改为 `SettingsPO[]`,并相应地修改代码实现,如下所示:
```
public ActionResult selectAll() {
SettingsPO[] settingsPOS = settingsMapper.selectAll().toArray(new SettingsPO[0]);
return new ActionResult().success(settingsPOS);
}
```
这里通过调用 `toArray()` 方法将 `List<SettingsPO>` 转换为 `SettingsPO[]`,同时传入一个空数组作为参数,以指定返回数组的类型。
相关问题
public ActionResult selectAll() { SettingsPO[] settingsPOS = settingsMapper.selectAll().toArray(new SettingsPO[0]); return new ActionResult().success(settingsPOS); } 三条数据,将这三条数据改为数组
如果您的 `SettingsPO` 类型的数组已经存在,您可以直接将数据添加到数组中,如下所示:
```
SettingsPO[] settingsPOS = new SettingsPO[3];
settingsPOS[0] = new SettingsPO(/*设置第一条数据*/);
settingsPOS[1] = new SettingsPO(/*设置第二条数据*/);
settingsPOS[2] = new SettingsPO(/*设置第三条数据*/);
return new ActionResult().success(settingsPOS);
```
如果您需要从数据库中获取这三条数据,您可以使用以下代码:
```
List<SettingsPO> settingsPOList = settingsMapper.selectAll();
SettingsPO[] settingsPOS = new SettingsPO[settingsPOList.size()];
settingsPOList.toArray(settingsPOS);
return new ActionResult().success(settingsPOS);
```
在上面的代码中,我们首先从数据库中获取所有的 `SettingsPO` 对象,然后将它们存储在一个 List 中。接下来,我们创建一个大小为 `settingsPOList.size()` 的 `SettingsPO` 数组,并使用 `toArray()` 方法将 List 中的元素复制到数组中。最后,我们将数组作为成功操作的结果返回。
UserInfo userInfo=userProvider.get(); OrganizeEntity organize = organizeService.getById(userInfo.getDepartmentId()); TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMap = tzglInvestmentPlanService.selectProjectDetial(params,organize); for (String key : investmentPlanProjectMap.keySet()) { List<TzglInvestmentPlanProjectModel> projectList = investmentPlanProjectMap.get(key); for (TzglInvestmentPlanProjectModel investmentPlanProject: projectList) { investmentPlanProject.setInvestmentSubjectName(generaterSwapUtil.comSelectValues(investmentPlanProject.getInvestmentSubject())); } } //按照年份分组后,降序排序 TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMapDescending = new TreeMap<>(Comparator.reverseOrder()); investmentPlanProjectMapDescending.putAll(investmentPlanProjectMap); return ActionResult.success(investmentPlanProjectMapDescending);优化此代码
可以考虑以下优化:
1. 将获取用户信息和部门信息的代码提取出来,避免在每个方法里都重复获取。
```
UserInfo userInfo = userProvider.get();
OrganizeEntity organize = organizeService.getById(userInfo.getDepartmentId());
```
2. 使用 Stream API 和 lambda 表达式来简化代码。
```
TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMap = tzglInvestmentPlanService.selectProjectDetial(params,organize);
investmentPlanProjectMap.forEach((key, projectList) -> projectList.forEach(investmentPlanProject -> investmentPlanProject.setInvestmentSubjectName(generaterSwapUtil.comSelectValues(investmentPlanProject.getInvestmentSubject()))));
TreeMap<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMapDescending = new TreeMap<>(Comparator.reverseOrder());
investmentPlanProjectMapDescending.putAll(investmentPlanProjectMap);
return ActionResult.success(investmentPlanProjectMapDescending);
```
3. 不需要先将 `investmentPlanProjectMap` 存放到一个 TreeMap 中,再进行降序排序。可以直接使用 Stream API 进行分组和排序。
```
Map<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMap = tzglInvestmentPlanService.selectProjectDetial(params,organize);
investmentPlanProjectMap.forEach((key, projectList) -> projectList.forEach(investmentPlanProject -> investmentPlanProject.setInvestmentSubjectName(generaterSwapUtil.comSelectValues(investmentPlanProject.getInvestmentSubject()))));
Map<String, List<TzglInvestmentPlanProjectModel>> investmentPlanProjectMapDescending = investmentPlanProjectMap.entrySet().stream()
.sorted(Map.Entry.<String, List<TzglInvestmentPlanProjectModel>>comparingByKey().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, TreeMap::new));
return ActionResult.success(investmentPlanProjectMapDescending);
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)