未验证 提交 02c4ce26 编写于 作者: G Golden Looly 提交者: GitHub

Merge pull request #1555 from dahuoyzs/v5-dev

劳动节快乐!补充部分方法
......@@ -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);
}
/**
* 获取匹配规则定义中匹配到元素的所有位置
*
......
......@@ -586,6 +586,17 @@ public class Validator {
return NumberUtil.isNumber(value);
}
/**
* 是否包含数字
*
* @param value 当前字符串
* @return boolean 是否存在数字
* @since 5.6.5
*/
public static boolean hasNumber(CharSequence value) {
return ReUtil.contains(PatternPool.NUMBERS, value);
}
/**
* 验证是否为数字
*
......
......@@ -4,6 +4,7 @@ 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;
......@@ -765,6 +766,50 @@ 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
/**
......@@ -868,6 +913,50 @@ 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
/**
......@@ -1505,6 +1594,28 @@ 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,6 +765,18 @@ 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
/**
......@@ -1110,6 +1122,19 @@ 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
/**
......
......@@ -299,6 +299,46 @@ public class ReUtil {
return pattern.matcher(content).replaceFirst(StrUtil.EMPTY);
}
/**
* 删除匹配的最后一个内容
*
* @param regex 正则
* @param str 被匹配的内容
* @return 删除后剩余的内容
* @since 5.6.5
*/
public static String delLast(String regex, CharSequence str) {
if (StrUtil.hasBlank(regex, str)) {
return StrUtil.str(str);
}
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return delLast(pattern, str);
}
/**
* 删除匹配的最后一个内容
*
* @param pattern 正则
* @param str 被匹配的内容
* @return 删除后剩余的内容
* @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);
}
}
return StrUtil.str(str);
}
/**
* 删除匹配的全部内容
*
......@@ -724,4 +764,4 @@ public class ReUtil {
}
return builder.toString();
}
}
\ No newline at end of file
}
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);
}
}
......@@ -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");
......
......@@ -20,6 +20,18 @@ public class ValidatorTest {
Assert.assertTrue(Validator.isNumber("0.33323"));
}
@Test
public void hasNumberTest() throws Exception {
String var1 = "";
String var2 = "str";
String var3 = "180";
String var4 = "身高180体重180";
Assert.assertFalse(Validator.hasNumber(var1));
Assert.assertFalse(Validator.hasNumber(var2));
Assert.assertTrue(Validator.hasNumber(var3));
Assert.assertTrue(Validator.hasNumber(var4));
}
@Test
public void isLetterTest() {
Assert.assertTrue(Validator.isLetter("asfdsdsfds"));
......
......@@ -24,4 +24,127 @@ public class CharSequenceUtilTest {
result = CharSequenceUtil.addSuffixIfNot(str, " is Good");
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));
}
@Test
public void endWithChineseTest() throws Exception {
String var1 = "str";
String var2 = "_str";
String var3 = "123";
String var4 = "180公斤";
Assert.assertFalse(CharSequenceUtil.endWithChinese(var1));
Assert.assertFalse(CharSequenceUtil.endWithChinese(var2));
Assert.assertFalse(CharSequenceUtil.endWithChinese(var3));
Assert.assertTrue(CharSequenceUtil.endWithChinese(var4));
}
// ------------------------------------------------------------------------ remove
@Test
public void removeNumbersTest(){
String var1 = "";
String var2 = "str";
String var3 = "身高180";
String var4 = "身高180体重180";
Assert.assertEquals("", CharSequenceUtil.removeNumbers(var1));
Assert.assertEquals("str", CharSequenceUtil.removeNumbers(var2));
Assert.assertEquals("身高", CharSequenceUtil.removeNumbers(var3));
Assert.assertEquals("身高体重", CharSequenceUtil.removeNumbers(var4));
}
@Test
public void removeChineseTest(){
String var1 = "";
String var2 = "str";
String var3 = "身高180";
String var4 = "身高180体重180cm";
Assert.assertEquals("", CharSequenceUtil.removeChinese(var1));
Assert.assertEquals("str", CharSequenceUtil.removeChinese(var2));
Assert.assertEquals("180", CharSequenceUtil.removeChinese(var3));
Assert.assertEquals("180180cm", CharSequenceUtil.removeChinese(var4));
}
}
......@@ -178,10 +178,20 @@ public class NumberUtilTest {
Assert.assertTrue(NumberUtil.equals(new BigDecimal("0.00"), BigDecimal.ZERO));
}
@Test
public void percentTest(){
Assert.assertEquals(30, NumberUtil.percent(3, 10));
Assert.assertEquals(20, NumberUtil.percent(1, 5));
}
@Test
public void formatPercentTest() {
String str = NumberUtil.formatPercent(0.33543545, 2);
Assert.assertEquals("33.54%", str);
Assert.assertEquals("30%", NumberUtil.formatPercent(3, 10, 0));
Assert.assertEquals("33.33%", NumberUtil.formatPercent(1, 3, 2));
Assert.assertEquals("33.333%", NumberUtil.formatPercent(1, 3, 3));
}
@Test
......
package cn.hutool.core.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.PatternPool;
import org.junit.Assert;
import org.junit.Test;
......@@ -23,7 +24,7 @@ public class ReUtilTest {
String resultExtractMulti = ReUtil.extractMulti("(\\w)aa(\\w)", content, "$1-$2");
Assert.assertEquals("Z-a", resultExtractMulti);
}
@Test
public void extractMultiTest2() {
// 抽取多个分组然后把它们拼接起来
......@@ -37,7 +38,32 @@ public class ReUtilTest {
String resultDelFirst = ReUtil.delFirst("(\\w)aa(\\w)"