mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
StrUtil类添加wordCount方法✒️
This commit is contained in:
parent
a1dba4f36b
commit
bc9ef1994b
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
import cn.hutool.core.text.StrPool;
|
||||
@ -9,6 +10,8 @@ import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -469,4 +472,63 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
||||
}
|
||||
return template2;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------ wordCount
|
||||
|
||||
/**
|
||||
* 统计 字符串 中单词出现次数(不排序)
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param separator 分隔符
|
||||
* @return Map<String, Long> 统计次数 如: {"hello":10}
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static Map<String, Long> wordCount(String str, String separator) {
|
||||
return wordCount(Collections.singletonList(str), separator);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计 字符串 中单词出现次数(根据value排序)
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param separator 分隔符
|
||||
* @param isValueDesc 是否倒叙排列
|
||||
* @return Map<String, Long> 统计次数 如: {"hello":10}
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static Map<String, Long> wordCount(String str, String separator, boolean isValueDesc) {
|
||||
return wordCount(Collections.singletonList(str), separator, isValueDesc);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计list中单词出现次数(不排序)
|
||||
*
|
||||
* @param list list容器
|
||||
* @param separator 分隔符
|
||||
* @return Map<String, Long> 统计次数 如: {"hello":10}
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static Map<String, Long> wordCount(List<String> list, String separator) {
|
||||
Map<String, Long> countMap = MapUtil.newHashMap();
|
||||
for (String str : list) {
|
||||
String[] words = str.split(separator);
|
||||
for (String word : words) {
|
||||
countMap.put(word, countMap.getOrDefault(word, 0L) + 1);
|
||||
}
|
||||
}
|
||||
return countMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计 字符串list 中单词出现次数(根据value排序)
|
||||
*
|
||||
* @param list list容器
|
||||
* @param separator 分隔符
|
||||
* @param isValueDesc 是否根据value倒叙排列
|
||||
* @return Map<String, Long> 统计次数 如: {"hello":10}
|
||||
* @since 5.6.5
|
||||
*/
|
||||
public static Map<String, Long> wordCount(List<String> list, String separator, boolean isValueDesc) {
|
||||
return MapUtil.sortByValue(wordCount(list, separator), isValueDesc);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,9 @@ import cn.hutool.core.lang.Dict;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 字符串工具类单元测试
|
||||
@ -508,4 +510,21 @@ public class StrUtilTest {
|
||||
Assert.assertEquals("jackduan@163.com", StrUtil.hide("jackduan@163.com", 16, 16));
|
||||
Assert.assertEquals("jackduan@163.com", StrUtil.hide("jackduan@163.com", 16, 17));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wordCountTest(){
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("Word Count");
|
||||
list.add("Hello world");
|
||||
list.add("Hello java");
|
||||
list.add("Hello Hutool");
|
||||
list.add("A set of tools that keep Java sweet");
|
||||
Map<String, Long> listCountMap = StrUtil.wordCount(list, " ");
|
||||
Assert.assertEquals(3L, listCountMap.get("Hello").longValue());
|
||||
|
||||
String singleton = "Can you can a can as a canner can can a can ?";
|
||||
Map<String, Long> strCountMap = StrUtil.wordCount(singleton, " ");
|
||||
Assert.assertEquals(5L, strCountMap.get("can").longValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user