提交 660c8c58 编写于 作者: L Looly

add methods

上级 02c4ce26
......@@ -10,6 +10,7 @@
* 【core 】 NumberUtil增加方法decimalFormat重载(issue#I3OSA2@Gitee)
* 【extra 】 Ftp的remoteVerificationEnabled改为false(issue#I3OSA2@Gitee)
* 【core 】 MaskBit增加掩码反向转换的方法getMaskBit()(pr#1563@Github)
* 【core 】 ReUtil等增加indexOf、delLast等方法(pr#1555@Github)
### 🐞Bug修复
* 【core 】 修复createScheduledExecutor单位不是毫秒的问题(issue#I3OYIW@Gitee)
......
......@@ -162,16 +162,10 @@ public class IterUtil {
public static <T> Map<T, Integer> countMap(Iterator<T> iter) {
final HashMap<T, Integer> countMap = new HashMap<>();
if (null != iter) {
Integer count;
T t;
while (iter.hasNext()) {
t = iter.next();
count = countMap.get(t);
if (null == count) {
countMap.put(t, 1);
} else {
countMap.put(t, count + 1);
}
countMap.put(t, countMap.getOrDefault(t, 0) + 1);
}
}
return countMap;
......
......@@ -5,14 +5,25 @@ 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.*;
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.concurrent.CopyOnWriteArrayList;
/**
* List相关工具类
*
* @author looly
*/
public class ListUtil {
/**
* 新建一个空List
......@@ -444,33 +455,6 @@ 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);
}
/**
* 获取匹配规则定义中匹配到元素的所有位置
*
......
......@@ -4,7 +4,6 @@ import cn.hutool.core.comparator.VersionComparator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
......@@ -766,50 +765,6 @@ public class CharSequenceUtil {
return false;
}
/**
* 字符串是否以(数字)开始
*
* @param str 字符串
* @return 是否数字开始
* @since 5.6.5
*/
public static boolean startWithNumber(CharSequence str) {
return isNotBlank(str) && PatternPool.NUMBERS.matcher(str.subSequence(0, 1)).find();
}
/**
* 字符串是否以(英文字母 、数字和下划线)开始
*
* @param str 字符串
* @return 是否英文字母 、数字和下划线开始
* @since 5.6.5
*/
public static boolean startWithGeneral(CharSequence str) {
return isNotBlank(str) && PatternPool.GENERAL.matcher(str.subSequence(0, 1)).find();
}
/**
* 字符串是否以(字母)开始
*
* @param str 字符串
* @return 是否字母开始
* @since 5.6.5
*/
public static boolean startWithWord(CharSequence str) {
return isNotBlank(str) && PatternPool.WORD.matcher(str.subSequence(0, 1)).find();
}
/**
* 字符串是否以(中文汉字)开始
*
* @param str 字符串
* @return 是否中文汉字开始
* @since 5.6.5
*/
public static boolean startWithChinese(CharSequence str) {
return isNotBlank(str) && PatternPool.CHINESES.matcher(str.subSequence(0, 1)).find();
}
// ------------------------------------------------------------------------ endWith
/**
......@@ -913,50 +868,6 @@ public class CharSequenceUtil {
return false;
}
/**
* 字符串是否以(数字)结束
*
* @param str 字符串
* @return 是否数字结束
* @since 5.6.5
*/
public static boolean endWithNumber(CharSequence str) {
return isNotBlank(str) && PatternPool.NUMBERS.matcher(str.subSequence(str.length() - 1, str.length())).find();
}
/**
* 字符串是否以(英文字母 、数字和下划线)结束
*
* @param str 字符串
* @return 是否英文字母 、数字和下划线结束
* @since 5.6.5
*/
public static boolean endWithGeneral(CharSequence str) {
return isNotBlank(str) && PatternPool.GENERAL.matcher(str.subSequence(str.length() - 1, str.length())).find();
}
/**
* 字符串是否以(字母)结束
*
* @param str 字符串
* @return 是否字母结束
* @since 5.6.5
*/
public static boolean endWithWord(CharSequence str) {
return isNotBlank(str) && PatternPool.WORD.matcher(str.subSequence(str.length() - 1, str.length())).find();
}
/**
* 字符串是否以(中文汉字)结束
*
* @param str 字符串
* @return 是否中文汉字结束
* @since 5.6.5
*/
public static boolean endWithChinese(CharSequence str) {
return isNotBlank(str) && PatternPool.CHINESES.matcher(str.subSequence(str.length() - 1, str.length())).find();
}
// ------------------------------------------------------------------------ contains
/**
......@@ -1594,28 +1505,6 @@ public class CharSequenceUtil {
return str2;
}
/**
* 剔除/移除字符串中的所有数字
*
* @param str 当前字符串
* @return 移除数字后的字符串
* @since 5.6.5
*/
public static String removeNumbers(CharSequence str) {
return ReUtil.delAll(PatternPool.NUMBERS, str);
}
/**
* 剔除/移除字符串中的所有中文
*
* @param str 当前字符串
* @return 移除中文后的字符串
* @since 5.6.5
*/
public static String removeChinese(CharSequence str) {
return ReUtil.delAll(PatternPool.CHINESES, str);
}
/**
* 清理空白字符
*
......
......@@ -765,18 +765,6 @@ public class NumberUtil {
return (int) Math.ceil((double) v1 / v2);
}
/**
* 求百分比(取整) (3,10) => 30
*
* @param num 当前num
* @param total 总长度
* @return int 百分比(取整)
* @since 5.6.5
*/
public static int percent(int num, int total) {
return (int) ((float) num / (float) total * 100);
}
// ------------------------------------------------------------------------------------------- round
/**
......@@ -1122,19 +1110,6 @@ public class NumberUtil {
return format.format(number);
}
/**
* 求百分比(带精度)(带百分号后缀) (3,10,0) => 30%
*
* @param num 当前num
* @param total 总长度
* @param scale 精度(保留小数点后几位)
* @return String 百分比(带百分号后缀)
* @since 5.6.5
*/
public static String formatPercent(Number num, Number total, int scale) {
return formatPercent(num.doubleValue() / total.doubleValue(), scale);
}
// ------------------------------------------------------------------------------------------- isXXX
/**
......
......@@ -14,6 +14,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -292,11 +293,24 @@ public class ReUtil {
* @return 删除后剩余的内容
*/
public static String delFirst(Pattern pattern, CharSequence content) {
if (null == pattern || StrUtil.isBlank(content)) {
return replaceFirst(pattern, content, StrUtil.EMPTY);
}
/**
* 替换匹配的第一个内容
*
* @param pattern 正则
* @param content 被匹配的内容
* @param replacement 替换的内容
* @return 替换后剩余的内容
* @since 5.6.5
*/
public static String replaceFirst(Pattern pattern, CharSequence content, String replacement) {
if (null == pattern || StrUtil.isEmpty(content)) {
return StrUtil.str(content);
}
return pattern.matcher(content).replaceFirst(StrUtil.EMPTY);
return pattern.matcher(content).replaceFirst(replacement);
}
/**
......@@ -325,14 +339,10 @@ public class ReUtil {
* @since 5.6.5
*/
public static String delLast(Pattern pattern, CharSequence str) {
if (null != pattern && StrUtil.isNotBlank(str)) {
String last = "";
for (Matcher matcher = pattern.matcher(str); matcher.find(); ) {
last = matcher.group();
}
if (StrUtil.isNotBlank(last)){
return StrUtil.subBefore(str, last, Boolean.TRUE) + StrUtil.subAfter(str, last, Boolean.TRUE);
if (null != pattern && StrUtil.isNotEmpty(str)) {
final MatchResult matchResult = lastIndexOf(pattern, str);
if(null != matchResult){
return StrUtil.subPre(str, matchResult.start()) + StrUtil.subSuf(str, matchResult.end());
}
}
......@@ -580,6 +590,79 @@ public class ReUtil {
return pattern.matcher(content).find();
}
/**
* 找到指定正则匹配到字符串的开始位置
*
* @param regex 正则
* @param content 字符串
* @return 位置,{@code null}表示未找到
* @since 5.6.5
*/
public static MatchResult indexOf(String regex, CharSequence content){
if (null == regex || null == content) {
return null;
}
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return indexOf(pattern, content);
}
/**
* 找到指定模式匹配到字符串的开始位置
*
* @param pattern 模式
* @param content 字符串
* @return 位置,{@code null}表示未找到
* @since 5.6.5
*/
public static MatchResult indexOf(Pattern pattern, CharSequence content){
if(null != pattern && null != content){
final Matcher matcher = pattern.matcher(content);
if(matcher.find()){
return matcher.toMatchResult();
}
}
return null;
}
/**
* 找到指定正则匹配到第一个字符串的位置
*
* @param regex 正则
* @param content 字符串
* @return 位置,{@code null}表示未找到
* @since 5.6.5
*/
public static MatchResult lastIndexOf(String regex, CharSequence content){
if (null == regex || null == content) {
return null;
}
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return lastIndexOf(pattern, content);
}
/**
* 找到指定模式匹配到最后一个字符串的位置
*
* @param pattern 模式
* @param content 字符串
* @return 位置,{@code null}表示未找到
* @since 5.6.5
*/
public static MatchResult lastIndexOf(Pattern pattern, CharSequence content){
MatchResult result = null;
if(null != pattern && null != content){
final Matcher matcher = pattern.matcher(content);
while(matcher.find()){
result = matcher.toMatchResult();
}
}
return result;
}
/**
* 从字符串中获得第一个整数
*
......
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;
......@@ -10,8 +9,6 @@ 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;
/**
......@@ -472,63 +469,4 @@ 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);
}
}
package cn.hutool.core.collection;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Filter;
......
......@@ -10,7 +10,6 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ListUtilTest {
......@@ -47,27 +46,6 @@ 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");
......
......@@ -25,126 +25,5 @@ public class CharSequenceUtilTest {
Assert.assertEquals( str + " is Good", result);
}
@Test
public void startWithNumberTest() throws Exception {
String var1 = "123str";
String var2 = "180公斤";
String var3 = "str";
String var4 = "身高180";
Assert.assertTrue(CharSequenceUtil.startWithNumber(var1));
Assert.assertTrue(CharSequenceUtil.startWithNumber(var2));
Assert.assertFalse(CharSequenceUtil.startWithNumber(var3));
Assert.assertFalse(CharSequenceUtil.startWithNumber(var4));
}
@Test
public void startWithGeneralTest() throws Exception {
String var1 = "str";
String var2 = "123";
String var3 = "_str";
String var4 = "身高180";
Assert.assertTrue(CharSequenceUtil.startWithGeneral(var1));
Assert.assertTrue(CharSequenceUtil.startWithGeneral(var2));
Assert.assertTrue(CharSequenceUtil.startWithGeneral(var3));
Assert.assertFalse(CharSequenceUtil.startWithGeneral(var4));
}
@Test
public void startWithWordTest() throws Exception {
String var1 = "str";
String var2 = "123";
String var3 = "_str";
String var4 = "身高180";
Assert.assertTrue(CharSequenceUtil.startWithWord(var1));
Assert.assertFalse(CharSequenceUtil.startWithWord(var2));
Assert.assertFalse(CharSequenceUtil.startWithWord(var3));
Assert.assertFalse(CharSequenceUtil.startWithWord(var4));
}
@Test
public void startWithChineseTest() throws Exception {
String var1 = "str";
String var2 = "_str";
String var3 = "123";
String var4 = "身高180";
Assert.assertFalse(CharSequenceUtil.startWithChinese(var1));
Assert.assertFalse(CharSequenceUtil.startWithChinese(var2));
Assert.assertFalse(CharSequenceUtil.startWithChinese(var3));
Assert.assertTrue(CharSequenceUtil.startWithChinese(var4));
}
@Test
public void endWithNumberTest() throws Exception {
String var1 = "str123";
String var2 = "身高180";
String var3 = "str";
String var4 = "180公斤";
Assert.assertTrue(CharSequenceUtil.endWithNumber(var1));
Assert.assertTrue(CharSequenceUtil.endWithNumber(var2));
Assert.assertFalse(CharSequenceUtil.endWithNumber(var3));
Assert.assertFalse(CharSequenceUtil.endWithNumber(var4));
}
@Test
public void endWithGeneralTest() throws Exception {
String var1 = "str";
String var2 = "123";
String var3 = "str_";
String var4 = "180公斤";
Assert.assertTrue(CharSequenceUtil.endWithGeneral(var1));
Assert.assertTrue(CharSequenceUtil.endWithGeneral(var2));
Assert.assertTrue(CharSequenceUtil.endWithGeneral(var3));
Assert.assertFalse(CharSequenceUtil.endWithGeneral(var4));
}
@Test
public void endWithWordTest() throws Exception {
String var1 = "str";
String var2 = "_str";
String var3 = "123";
String var4 = "身高180";
Assert.assertTrue(CharSequenceUtil.endWithWord(var1));
Assert.assertTrue(CharSequenceUtil.endWithWord(var2));
Assert.assertFalse(CharSequenceUtil.endWithWord(var3));
Assert.assertFalse(CharSequenceUtil.endWithWord(var4));
}