diff --git a/CHANGELOG.md b/CHANGELOG.md index 08f48f250..032ef01ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,13 @@ ------------------------------------------------------------------------------------------------------------- -# 5.4.7 (2020-10-23) +# 5.4.7 (2020-10-27) ### 新特性 * 【core 】 增加OptionalBean(pr#1182@Github) * 【core 】 Ganzhi增加方法(issue#1186@Github) +* 【core 】 CollUtil增加forEach重载(issue#I22NA4@Gitee) +* 【core 】 CollUtil.map忽略空值改规则为原数组中的元素和处理后的元素都会忽略空值(issue#I22N08@Gitee) ### Bug修复 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 1b5f93afa..dffb2a987 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -1371,7 +1371,7 @@ public class CollUtil { * @param 返回集合元素类型 * @param collection 原集合 * @param func 编辑函数 - * @param ignoreNull 是否忽略空值 + * @param ignoreNull 是否忽略空值,这里的空值包括函数处理前和处理后的null值 * @return 抽取后的新列表 * @since 5.3.5 */ @@ -1382,8 +1382,11 @@ public class CollUtil { } R value; - for (T bean : collection) { - value = func.apply(bean); + for (T t : collection) { + if(null == t && ignoreNull){ + continue; + } + value = func.apply(t); if (null == value && ignoreNull) { continue; } @@ -2545,6 +2548,18 @@ public class CollUtil { // ------------------------------------------------------------------------------------------------- forEach + /** + * 循环遍历 {@link Iterable},使用{@link Consumer} 接受遍历的每条数据,并针对每条数据做处理 + * + * @param 集合元素类型 + * @param iterable {@link Iterable} + * @param consumer {@link Consumer} 遍历的每条数据处理器 + * @since 5.4.7 + */ + public static void forEach(Iterable iterable, Consumer consumer) { + forEach(iterable.iterator(), consumer); + } + /** * 循环遍历 {@link Iterator},使用{@link Consumer} 接受遍历的每条数据,并针对每条数据做处理 * @@ -2879,7 +2894,8 @@ public class CollUtil { // ---------------------------------------------------------------------------------------------- Interface start /** - * 针对一个参数做相应的操作 + * 针对一个参数做相应的操作
+ * 此函数接口与JDK8中Consumer不同是多提供了index参数,用于标记遍历对象是第几个。 * * @param 处理参数类型 * @author Looly diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 8ea1a51fe..b2525c375 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -10,7 +10,20 @@ import lombok.Data; import org.junit.Assert; import org.junit.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.SortedSet; /** * 集合工具类单元测试