From a1dba4f36bb85e6f0eb49a2a9835c5f84845f603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=81=AByzs?= <835476090@qq.com> Date: Tue, 4 May 2021 15:01:43 +0800 Subject: [PATCH] =?UTF-8?q?ListUtil=E7=B1=BB=E6=B7=BB=E5=8A=A0countMap?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E2=9C=92=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/collection/ListUtil.java | 37 +++++++++++++++---- .../hutool/core/collection/ListUtilTest.java | 22 +++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index 088e19787..d6a3093d2 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -5,18 +5,12 @@ import cn.hutool.core.comparator.PropertyComparator; import cn.hutool.core.convert.Convert; import cn.hutool.core.lang.Editor; import cn.hutool.core.lang.Matcher; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.PageUtil; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; public class ListUtil { @@ -450,6 +444,33 @@ public class ListUtil { return list2; } + /** + * 统计list中元素出现次数 + * + * @param list list容器 + * @return Map 统计次数 如: {"hello":10} + * @since 5.6.5 + */ + public static Map countMap(List list) { + Map countMap = MapUtil.newHashMap(); + for (T o : list) { + countMap.put(o, countMap.getOrDefault(o, 0L) + 1); + } + return countMap; + //return list.stream().collect(Collectors.groupingBy(o -> o, Collectors.counting()));//stream方式 + } + + /** + * 统计list中元素出现次数 + * + * @param list list容器 + * @return Map 统计次数 如: {"hello":10} + * @since 5.6.5 + */ + public static Map countMap(List list, boolean isValueDesc) { + return MapUtil.sortByValue(countMap(list), isValueDesc); + } + /** * 获取匹配规则定义中匹配到元素的所有位置 * diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java index 46c94cdd5..17f413a0a 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java @@ -10,6 +10,7 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; +import java.util.Map; public class ListUtilTest { @@ -46,6 +47,27 @@ public class ListUtilTest { Assert.assertEquals("edit3", filter.get(2)); } + @Test + @Ignore + public void countMapTest(){ + List list = new ArrayList<>(); + list.add("AAA"); + list.add("BBB"); + list.add("AAA"); + list.add("CCC"); + list.add("DDD"); + list.add("DDD"); + //统计不排序 + Map countMap = ListUtil.countMap(list); + Console.log(countMap); + //统计倒序排列 + Map descCountMap = ListUtil.countMap(list, Boolean.TRUE); + Console.log(descCountMap); + //统计正序排列 + Map ascCountMap= ListUtil.countMap(list, Boolean.FALSE); + Console.log(ascCountMap); + } + @Test public void indexOfAll() { List a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");