From 3f7f118d3e6b38c2cb13a2661a37eda3325894a7 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 17 Jul 2025 20:23:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=96=B0=E5=A2=9E=E9=9B=86?= =?UTF-8?q?=E5=90=88=E5=B7=A5=E5=85=B7=E7=B1=BB=20CollUtils=EF=BC=88mapToL?= =?UTF-8?q?ist=E3=80=81mapToSet=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../continew/starter/core/util/CollUtils.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 continew-starter-core/src/main/java/top/continew/starter/core/util/CollUtils.java diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/util/CollUtils.java b/continew-starter-core/src/main/java/top/continew/starter/core/util/CollUtils.java new file mode 100644 index 00000000..233c6eca --- /dev/null +++ b/continew-starter-core/src/main/java/top/continew/starter/core/util/CollUtils.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.core.util; + +import cn.hutool.core.collection.CollUtil; + +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * 集合相关工具类 + * + * @author Charles7c + * @since 2.13.1 + */ +public class CollUtils { + + private CollUtils() { + } + + /** + * 通过 func 自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的列表返回
+ * 例如:提供一个 Bean 列表,通过 Function 接口实现获取某个字段值,返回这个字段值组成的新列表 + * + * @param 集合元素类型 + * @param 返回集合元素类型 + * @param collection 原集合 + * @param func 编辑函数 + * @return 抽取后的新列表(默认去除 null 值) + * @see CollUtil#map(Iterable, Function, boolean) + */ + public static List mapToList(Collection collection, Function func) { + return mapToList(collection, func, true); + } + + /** + * 通过 func 自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的列表返回
+ * 例如:提供一个 Bean 列表,通过 Function 接口实现获取某个字段值,返回这个字段值组成的新列表 + * + * @param 集合元素类型 + * @param 返回集合元素类型 + * @param collection 原集合 + * @param func 编辑函数 + * @param ignoreNull 是否忽略空值,这里的空值包括函数处理前和处理后的 null 值 + * @return 抽取后的新列表 + * @see CollUtil#map(Iterable, Function, boolean) + */ + public static List mapToList(Collection collection, + Function func, + boolean ignoreNull) { + if (CollUtil.isEmpty(collection)) { + return new ArrayList<>(0); + } + Stream stream = collection.stream(); + if (ignoreNull) { + return stream.filter(Objects::nonNull).map(func).filter(Objects::nonNull).collect(Collectors.toList()); + } + return stream.map(func).collect(Collectors.toList()); + } + + /** + * 通过 func 自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的集合返回
+ * 例如:提供一个 Bean 集合,通过 Function 接口实现获取某个字段值,返回这个字段值组成的新集合 + * + * @param 集合元素类型 + * @param 返回集合元素类型 + * @param collection 原集合 + * @param func 编辑函数 + * @return 抽取后的新集合(默认去除 null 值) + * @see CollUtil#map(Iterable, Function, boolean) + */ + public static Set mapToSet(Collection collection, Function func) { + return mapToSet(collection, func, true); + } + + /** + * 通过 func 自定义一个规则,此规则将原集合中的元素转换成新的元素,生成新的集合返回
+ * 例如:提供一个 Bean 集合,通过 Function 接口实现获取某个字段值,返回这个字段值组成的新集合 + * + * @param 集合元素类型 + * @param 返回集合元素类型 + * @param collection 原集合 + * @param func 编辑函数 + * @param ignoreNull 是否忽略空值,这里的空值包括函数处理前和处理后的 null 值 + * @return 抽取后的新集合 + * @see CollUtil#map(Iterable, Function, boolean) + */ + public static Set mapToSet(Collection collection, + Function func, + boolean ignoreNull) { + if (CollUtil.isEmpty(collection)) { + return new HashSet<>(0); + } + Stream stream = collection.stream(); + if (ignoreNull) { + return stream.filter(Objects::nonNull).map(func).filter(Objects::nonNull).collect(Collectors.toSet()); + } + return stream.map(func).collect(Collectors.toSet()); + } +}