From 508c139b224c8414f5828698cd58ec557366c5e1 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 4 Sep 2022 22:09:01 +0800 Subject: [PATCH] =?UTF-8?q?MapUtil=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AEentr?= =?UTF-8?q?y=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../src/main/java/cn/hutool/core/map/MapUtil.java | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85d618ce3..a62bff564 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * 【extra 】 resource.loader等过期参数替换(issue#2571@Github) * 【core 】 添加ObjectUtil的别名工具类ObjUtil * 【core 】 扩展LocalDateTimeUtil.isIn方法使用场景(pr#2589@Github) +* 【core 】 MapUtil增加根据entry分组(pr#2591@Github) * ### 🐞Bug修复 * 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java index e8051d9e0..59bd904f7 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java @@ -107,7 +107,7 @@ public class MapUtil { * @since 3.0.4 */ public static HashMap newHashMap(int size, boolean isLinked) { - int initialCapacity = (int) (size / DEFAULT_LOAD_FACTOR) + 1; + final int initialCapacity = (int) (size / DEFAULT_LOAD_FACTOR) + 1; return isLinked ? new LinkedHashMap<>(initialCapacity) : new HashMap<>(initialCapacity); } @@ -504,19 +504,19 @@ public class MapUtil { * @return entries */ public static Map> grouping(Iterable> entries) { - final Map> map = new HashMap<>(DEFAULT_INITIAL_CAPACITY); + final Map> map = new HashMap<>(); if (CollUtil.isEmpty(entries)) { return map; } - for (Map.Entry pair : entries) { + for (final Map.Entry pair : entries) { + final List values; if (map.containsKey(pair.getKey())) { - List values = map.get(pair.getKey()); - values.add(pair.getValue()); + values = map.get(pair.getKey()); } else { - List values = CollUtil.newArrayList(); - values.add(pair.getValue()); + values = new ArrayList<>(); map.put(pair.getKey(), values); } + values.add(pair.getValue()); } return map; }