From 11327a368ff6d0537d630a578b0f38156c0a5cef Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 17 Oct 2020 22:50:31 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20Framework-API=20=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=B8=80=E4=B8=AA=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] CollectionUtils 添加集合工具类; --- .../bot/framework/util/CollectionUtils.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/util/CollectionUtils.java diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/util/CollectionUtils.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/util/CollectionUtils.java new file mode 100644 index 0000000..d40cdf3 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/util/CollectionUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 LamGC + * + * ContentGrabbingJi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ContentGrabbingJi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.lamgc.cgj.bot.framework.util; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * 额外的集合工具类. + * @author LamGC + */ +public class CollectionUtils { + + /** + * 通过 keySet 和取值接口构造对应 Map. + * 处理时会自动跳过 key 为 null 的情况. + * @param keySet 存储了所有 key 的 Set 对象. + * @param valueSource 可根据 key 获取 value 的接口. + * @param 键类型. + * @param 值类型. + * @return 返回对应 Map. + */ + public static Map toMap(Set keySet, Function valueSource) { + Map map = new HashMap<>(keySet.size()); + keySet.forEach(key -> { + if(key != null) { + map.put(key, valueSource.apply(key)); + } + }); + return map; + } + + +}