mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
ListUtil类添加countMap方法✒️
This commit is contained in:
parent
32e29438d7
commit
a1dba4f36b
@ -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<T, Long> 统计次数 如: {"hello":10}
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static <T> Map<T, Long> countMap(List<T> list) {
|
||||
Map<T, Long> 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<T, Long> 统计次数 如: {"hello":10}
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static <T> Map<T, Long> countMap(List<T> list, boolean isValueDesc) {
|
||||
return MapUtil.sortByValue(countMap(list), isValueDesc);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取匹配规则定义中匹配到元素的所有位置
|
||||
*
|
||||
|
@ -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<String> list = new ArrayList<>();
|
||||
list.add("AAA");
|
||||
list.add("BBB");
|
||||
list.add("AAA");
|
||||
list.add("CCC");
|
||||
list.add("DDD");
|
||||
list.add("DDD");
|
||||
//统计不排序
|
||||
Map<?, Long> countMap = ListUtil.countMap(list);
|
||||
Console.log(countMap);
|
||||
//统计倒序排列
|
||||
Map<String, Long> descCountMap = ListUtil.countMap(list, Boolean.TRUE);
|
||||
Console.log(descCountMap);
|
||||
//统计正序排列
|
||||
Map<String, Long> ascCountMap= ListUtil.countMap(list, Boolean.FALSE);
|
||||
Console.log(ascCountMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void indexOfAll() {
|
||||
List<String> a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");
|
||||
|
Loading…
x
Reference in New Issue
Block a user