首发公众号:赵侠客
引言
在前面《使用Hutool要注意了!升级到6.0后你调用的所有方法都将报错》一文中我发现Hutool从5.0升级到6.0后有很多的不兼容,所有的包名都改了,也盘点了很多不兼容的方法,接下几篇文章中我将盘点Hutool6.0里新增加的方法。
二、MapUtil
2.1 MapUtil.ofKvs()
作用: 快速构建出HashMap
@Test
public void testOfKvs() {
Map<String, String> hashMap = MapUtil.ofKvs(true, "name", "公众号", "userName", "赵侠客");
Map<String, String> linkHashMap = MapUtil.ofKvs(false, "name", "公众号", "userName", "赵侠客");
Assertions.assertInstanceOf(LinkedHashMap.class, hashMap);
Assertions.assertInstanceOf(HashMap.class, linkHashMap);
}
注意:
- 第一个参数为true时构建出的是LinkedHashMap,false时构建出的是HashMap
- 不像Map.of()构建出的是不可变集合,Hutool的是可变的集合
2.2 MapUtil.removeByValue()
作用: 通过Value删除Map中的元素
@Test
public void testRemoveByValue(){
Map<String, String> hashMap = MapUtil.ofKvs(true, "name", "公众号", "userName", "赵侠客","accountName","赵侠客");
MapUtil.removeByValue(hashMap, "赵侠客");
Assertions.assertTrue(hashMap.size()==1);
}
注意:
- 主要解决原生Map只能通过Key删除,不能通过Value删除
- 当有多个value时所有相同的Value都会被删除
- 相比通过Key删除时间复杂为为O(1),通过Value删除时间复杂度为O(n)
2.3 MapUtil.firstMatchValue()
作用: 通过条件找到第一个满足条件的Value
@Test
public void testFirstMatchValue() {
Map<String, String> hashMap = MapUtil.ofKvs(true, "name", "公众号", "userName", "赵侠客", "accountName", "赵侠客");
String value = MapUtil.firstMatchValue(hashMap, x -> "userName".equals(x.getKey()));
Assertions.assertEquals(value,"赵侠客");
}
注意:
- 可能是想解决Map中存储对象时,无法通过对象中的值找到该对象的问题
2.4 MapUtil.firstMatch()
作用: 通过条件找到第一个满足条件的元素
@Test
public void testFirstMatch() {
Map<String, String> hashMap = MapUtil.ofKvs(true, "name", "公众号", "userName", "赵侠客", "accountName", "赵侠客");
Map.Entry<String,String> stringStringEntry = MapUtil.firstMatch(hashMap, x -> "赵侠客".equals(x.getValue()));
Assertions.assertEquals(stringStringEntry.getKey(),"userName");
}
注意:
- 和firstMatchValue不同的是返回一个Map.Entry()
三、NumberUtil
3.1 NumberUtil.formatThousands()
作用: 按千分位格式化数字
@Test
public void testFormatThousands(){
double num=1003.14159265358979323846;
String formatNum= NumberUtil.formatThousands(num,2);
Assertions.assertEquals("1,003.14",formatNum);
}
注意:
- 可以对所有数字类型按千分位格式化
3.2 NumberUtil.parseBigInteger()
作用: 将字符串转成BigInteger
@Test
public void testParseBigInteger(){
BigInteger bigNum=new BigInteger("999999999999999999999999999999");
Assertions.assertEquals(NumberUtil.parseBigInteger("999999999999999999999999999999"),bigNum);
Assertions.assertEquals(NumberUtil.parseBigInteger("-999999999999999999999999999999"),bigNum.multiply(BigInteger.valueOf(-1)));
Assertions.assertEquals(NumberUtil.parseBigInteger("0xF"),BigInteger.valueOf(15));
Assertions.assertEquals(NumberUtil.parseBigInteger("#F"),BigInteger.valueOf(15));
Assertions.assertEquals(NumberUtil.parseBigInteger("010"),BigInteger.valueOf(8));
}
注意:
- 支持正数及负数
- 默认是十进制,支持十六进制和八进制
3.3 NumberUtil.isZero()
作用: 判断一个数字是否为0
@Test
public void testIsZero(){
Integer integerZero=0;
Long longZero=0L;
Byte byteZero=0;
Short shortZero=0;
BigInteger bigIntegerZero=BigInteger.valueOf(0);
Float floatZero=0.0f;
Double doubleZero=0.0d;
Assertions.assertTrue(NumberUtil.isZero(integerZero));
Assertions.assertTrue(NumberUtil.isZero(longZero));
Assertions.assertTrue(NumberUtil.isZero(byteZero));
Assertions.assertTrue(NumberUtil.isZero(shortZero));
Assertions.assertTrue(NumberUtil.isZero(bigIntegerZero));
Assertions.assertTrue(NumberUtil.isZero(floatZero));
Assertions.assertTrue(NumberUtil.isZero(doubleZero));
}
注意:
- 支持所有的数字类型如Integer、Long、Byte、Short、BigInteger、Float、Double
3.4 NumberUtil.intToRoman()
作用: 将阿拉伯数字转成罗马数字
@Test
public void testIntToRoman(){
Assertions.assertEquals(NumberUtil.intToRoman(1),"I");
Assertions.assertEquals(NumberUtil.intToRoman(2),"II");
Assertions.assertEquals(NumberUtil.intToRoman(3),"III");
Assertions.assertEquals(NumberUtil.intToRoman(4),"IV");
Assertions.assertEquals(NumberUtil.intToRoman(5),"V");
Assertions.assertEquals(NumberUtil.intToRoman(6),"VI");
Assertions.assertEquals(NumberUtil.intToRoman(7),"VII");
Assertions.assertEquals(NumberUtil.intToRoman(10),"X");
Assertions.assertEquals(NumberUtil.intToRoman(50),"L");
Assertions.assertEquals(NumberUtil.intToRoman(100),"C");
Assertions.assertEquals(NumberUtil.intToRoman(500),"D");
Assertions.assertEquals(NumberUtil.intToRoman(1000),"M");
}
3.5 NumberUtil.romanToInt()
作用: 将罗马数字转成阿拉伯数字
@Test
public void testRomanToInt(){
Assertions.assertEquals(NumberUtil.romanToInt("I"),1);
Assertions.assertEquals(NumberUtil.romanToInt("II"),2);
Assertions.assertEquals(NumberUtil.romanToInt("III"),3);
Assertions.assertEquals(NumberUtil.romanToInt("IV"),4);
Assertions.assertEquals(NumberUtil.romanToInt("V"),5);
Assertions.assertEquals(NumberUtil.romanToInt("VI"),6);
Assertions.assertEquals(NumberUtil.romanToInt("VII"),7);
Assertions.assertEquals(NumberUtil.romanToInt("X"),10);
Assertions.assertEquals(NumberUtil.romanToInt("L"),50);
Assertions.assertEquals(NumberUtil.romanToInt("C"),100);
Assertions.assertEquals(NumberUtil.romanToInt("D"),500);
Assertions.assertEquals(NumberUtil.romanToInt("M"),1000);
}
四、TimeUtil
4.1 获取每月的开始结束时间
Assertions.assertEquals(TimeUtil.beginOfMonth(LocalDateTime.now()),LocalDateTime.parse("2024-08-01T00:00:00"));
Assertions.assertEquals(TimeUtil.beginOfMonth(LocalDate.now()),LocalDate.parse("2024-08-01"));
Assertions.assertEquals(TimeUtil.endOfMonth(LocalDateTime.now(),true),LocalDateTime.parse("2024-08-31T23:59:59"));
Assertions.assertEquals(TimeUtil.endOfMonth(LocalDate.now()),LocalDate.parse("2024-08-31"));
4.2 获取每年的开始结束时间
Assertions.assertEquals(TimeUtil.beginOfYear(LocalDateTime.now()),LocalDateTime.parse("2024-01-01T00:00:00"));
Assertions.assertEquals(TimeUtil.beginOfYear(LocalDate.now()),LocalDate.parse("2024-01-01"));
Assertions.assertEquals(TimeUtil.endOfYear(LocalDateTime.now(),true),LocalDateTime.parse("2024-12-31T23:59:59"));
Assertions.assertEquals(TimeUtil.endOfYear(LocalDate.now()),LocalDate.parse("2024-12-31"));
4.3 TimeUtil.ofPattern
作用: 从字符串快速生成DateTimeFormatter对象
DateTimeFormatter dateTimeFormatter=TimeUtil.ofPattern("yyyy-MM-dd HH:mm:ss");
//2024-08-05 15:04:24
String now= dateTimeFormatter.format(LocalDateTime.now());
4.4 TimeUtil.max
作用: 获取时分秒最大值
//23:59:59
System.out.println(LocalTimeUtil.max(true));
//23:59:59.999999999
System.out.println(LocalTimeUtil.max(false));
注意:
- 参数传true 毫秒归零
- 参数传false 毫秒为最大值
总结
本文主要介绍了Hutool6.0中MapUtill、NumberUtil、TimeUtil三个工具类中新添加的方法,并给出方法的作用及注意事项,每个方法都给出了详细的单元测试用例。Hutool6.0中还添加了很多其它的方法,未完待续,请关注公众号:赵侠客,持续更新中…