mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-11 16:57:12 +08:00
优化:基于阿里巴巴 Java 开发手册(黄山版)优化部分空集合处理
1.编程规约>集合处理>第7条:(个人理解:emptyList() 固然可以减少资源浪费,但未来由于不确定的需求变更,一旦增删元素必然会引发异常) 【强制】Collections 类返回的对象,如:emptyList() / singletonList() 等都是 immutable list,不可 对其进行添加或者删除元素的操作。 反例:如果查询无结果,返回 Collections.emptyList() 空集合对象,调用方一旦在返回的集合中进行了添加元素的操 作,就会触发 UnsupportedOperationException 异常。 2.编程规约>集合处理>第17条: 【推荐】集合初始化时,指定集合初始值大小。 说明:HashMap 使用构造方法 HashMap(int initialCapacity) 进行初始化时,如果暂时无法确定集合大小,那么指 定默认值(16)即可。 正例:initialCapacity = (需要存储的元素个数 / 负载因子) + 1。注意负载因子(即 loaderfactor)默认为 0.75,如果 暂时无法确定初始值大小,请设置为 16(即默认值)。 反例:HashMap 需要放置 1024 个元素,由于没有设置容量初始大小,随着元素增加而被迫不断扩容,resize() 方法 总共会调用 8 次,反复重建哈希表和数据迁移。当放置的集合元素个数达千万级时会影响程序性能。
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.service.impl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -60,7 +60,7 @@ public class RoleMenuServiceImpl implements RoleMenuService {
|
||||
@Override
|
||||
public List<Long> listMenuIdByRoleIds(List<Long> roleIds) {
|
||||
if (CollUtil.isEmpty(roleIds)) {
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
return roleMenuMapper.selectMenuIdsByRoleIds(roleIds);
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.service.impl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -141,7 +141,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
List<Long> menuIds = list.stream().map(MenuVO::getMenuId).collect(Collectors.toList());
|
||||
detailVO.setMenuIds(menuIds);
|
||||
} else {
|
||||
detailVO.setMenuIds(roleMenuService.listMenuIdByRoleIds(Collections.singletonList(roleId)));
|
||||
detailVO.setMenuIds(roleMenuService.listMenuIdByRoleIds(CollUtil.newArrayList(roleId)));
|
||||
}
|
||||
detailVO.setDeptIds(roleDeptService.listDeptIdByRoleId(roleId));
|
||||
}
|
||||
@@ -150,7 +150,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
@Override
|
||||
public List<LabelValueVO<Long>> buildDict(List<RoleVO> list) {
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
return list.stream().map(r -> new LabelValueVO<>(r.getRoleName(), r.getRoleId())).collect(Collectors.toList());
|
||||
}
|
||||
@@ -158,9 +158,6 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
@Override
|
||||
public List<String> listRoleNamesByRoleIds(List<Long> roleIds) {
|
||||
List<RoleDO> roleList = super.lambdaQuery().select(RoleDO::getRoleName).in(RoleDO::getRoleId, roleIds).list();
|
||||
if (CollUtil.isEmpty(roleList)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return roleList.stream().map(RoleDO::getRoleName).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -168,9 +165,6 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
public Set<String> listRoleCodesByUserId(Long userId) {
|
||||
List<Long> roleIds = userRoleService.listRoleIdsByUserId(userId);
|
||||
List<RoleDO> roleList = super.lambdaQuery().select(RoleDO::getRoleCode).in(RoleDO::getRoleId, roleIds).list();
|
||||
if (CollUtil.isEmpty(roleList)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return roleList.stream().map(RoleDO::getRoleCode).collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user