diff --git a/CHANGELOG.md b/CHANGELOG.md index a8c6f0685..c9cee9314 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * 【core 】 FileTypeUtil使用长匹配优先(pr#1457@Github) * 【core 】 IterUtil和CollUtil增加isEqualList方法(issue#I3A3PY@Gitee) * 【crypto 】 增加PBKDF2(issue#1416@Github) +* 【core 】 增加FuncKeyMap(issue#1402@Github) ### Bug修复 * 【socket 】 修复Client创建失败资源未释放问题。 diff --git a/hutool-core/src/main/java/cn/hutool/core/map/FuncKeyMap.java b/hutool-core/src/main/java/cn/hutool/core/map/FuncKeyMap.java new file mode 100644 index 000000000..e9589c9d8 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/map/FuncKeyMap.java @@ -0,0 +1,45 @@ +package cn.hutool.core.map; + +import java.util.Map; +import java.util.function.Function; + +/** + * 自定义函数Key风格的Map + * + * @param 键类型 + * @param 值类型 + * @author Looly + * @since 5.6.0 + */ +public class FuncKeyMap extends CustomKeyMap { + private static final long serialVersionUID = 1L; + + private Function keyFunc; + + // ------------------------------------------------------------------------- Constructor start + + /** + * 构造 + * + * @param m Map + * @param keyFunc 自定义KEY的函数 + */ + public FuncKeyMap(Map m, Function keyFunc) { + super(m); + } + // ------------------------------------------------------------------------- Constructor end + + /** + * 将Key转为驼峰风格,如果key为字符串的话 + * + * @param key KEY + * @return 驼峰Key + */ + @Override + protected Object customKey(Object key) { + if (null != this.keyFunc) { + return keyFunc.apply(key); + } + return key; + } +}