mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 22:57:17 +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:
		| @@ -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); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user