Java 代码优化:使用 Stream 和 Page 构造函数简化代码
这段代码已经相对简洁且高效,不过可以进行一些小的优化,如下:
- 使用Stream的
collect(Collectors.toList())替代手动创建ArrayList和循环添加元素,可以简化代码:
List<StoreAdminListRespVO> storeList = storePage.getRecords().stream().map(store -> {
StoreAdminListRespVO respVO = StoreConvert.INSTANCE.convert0(store);
respVO.setCreateUser(userMap.get(store.getCreateUser()));
return respVO;
}).collect(Collectors.toList());
- 使用
Page<T>的构造方法简化代码,可以直接传入storeList作为记录集合:
return new Page<>(storePage.getCurrent(), storePage.getSize(), storePage.getTotal(), storeList);
这些小的优化可以让代码更加简洁和易读。
原文地址: https://www.cveoy.top/t/topic/p8Sm 著作权归作者所有。请勿转载和采集!