优化:基于阿里巴巴 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:
parent
dc751fc3ec
commit
6da85463c9
@ -17,8 +17,8 @@
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -104,7 +104,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
|
||||
public List<Tree<Long>> tree(Q query, SortQuery sortQuery, boolean isSimple) {
|
||||
List<V> list = this.list(query, sortQuery);
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
// 如果构建简单树结构,则不包含基本树结构之外的扩展字段
|
||||
@ -205,7 +205,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
|
||||
queryWrapper.orderBy(order != null, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty()));
|
||||
}
|
||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||
return CollUtil.isNotEmpty(entityList) ? BeanUtil.copyToList(entityList, targetClass) : Collections.emptyList();
|
||||
return BeanUtil.copyToList(entityList, targetClass);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,4 +34,9 @@ public class CharConsts implements CharPool {
|
||||
* 分号
|
||||
*/
|
||||
public static final String SEMICOLON = ";";
|
||||
|
||||
/**
|
||||
* 空字符串
|
||||
*/
|
||||
public static final String EMPTY = "";
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ public class PageDataVO<V> {
|
||||
int fromIndex = (page - 1) * size;
|
||||
int toIndex = page * size + size;
|
||||
if (fromIndex > list.size()) {
|
||||
pageDataVO.setList(new ArrayList<>());
|
||||
pageDataVO.setList(new ArrayList<>(0));
|
||||
} else if (toIndex >= list.size()) {
|
||||
pageDataVO.setList(list.subList(fromIndex, list.size()));
|
||||
} else {
|
||||
|
@ -18,8 +18,8 @@ package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
@ -229,7 +229,7 @@ public class MailUtils {
|
||||
*/
|
||||
private static List<String> splitAddress(String addresses) {
|
||||
if (StrUtil.isBlank(addresses)) {
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
List<String> result;
|
||||
|
@ -24,10 +24,10 @@ import java.util.stream.Collectors;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.constant.CharConsts;
|
||||
|
||||
/**
|
||||
* Stream 工具类
|
||||
*
|
||||
@ -52,7 +52,7 @@ public class StreamUtils {
|
||||
*/
|
||||
public static <E> String join(Collection<E> collection, Function<E, String> function, CharSequence delimiter) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return StringUtils.EMPTY;
|
||||
return CharConsts.EMPTY;
|
||||
}
|
||||
return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter));
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
@ -79,7 +79,7 @@ public class TreeUtils {
|
||||
*/
|
||||
public static <T, E> List<Tree<E>> build(List<T> list, TreeNodeConfig treeNodeConfig, NodeParser<T, E> nodeParser) {
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
E parentId = (E)ReflectUtil.getFieldValue(list.get(0), treeNodeConfig.getParentIdKey());
|
||||
return TreeUtil.build(list, parentId, treeNodeConfig, nodeParser);
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user