当前位置: 首页 > news >正文

盘点Hutool6.0中新增的那些方法(上)

首发公众号:赵侠客

引言

在前面《使用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中还添加了很多其它的方法,未完待续,请关注公众号:赵侠客,持续更新中…

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Day6
  • 缓冲流练习
  • js第二天
  • 最强开源文生图模型一夜易主!SD一作、Stabililty AI核心成员Robin Rombach下场创业了,一出手就是王炸。
  • Python 爬虫项目实战(一):爬取某云热歌榜歌曲
  • E23.【C语言】练习:不创建第三个变量实现两个整数的交换
  • 锂电池生产工艺数字化的业务架构.pptx
  • 可视化图表与源代码显示的动态调整
  • vite静态资源处理,处理vite项目中src和url路径问题
  • DedeCMS-V5.7.82-UTF8织梦
  • 复现一下最近学习的漏洞(sqlab 1-10)
  • 速盾:爬虫攻击和cc攻击的区别是什么?
  • Git在日常项目中的实用技巧
  • 汉诺塔(C++)
  • 2024华数杯数学建模A题完整论文讲解(含每一问python代码+结果+可视化图)
  • __proto__ 和 prototype的关系
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • C++11: atomic 头文件
  • github指令
  • js对象的深浅拷贝
  • 从零开始学习部署
  • 给Prometheus造假数据的方法
  • 关于Flux,Vuex,Redux的思考
  • 缓存与缓冲
  • 利用DataURL技术在网页上显示图片
  • 前端
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 网络应用优化——时延与带宽
  • 阿里云ACE认证学习知识点梳理
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • ​VRRP 虚拟路由冗余协议(华为)
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • # 透过事物看本质的能力怎么培养?
  • #QT(串口助手-界面)
  • #数据结构 笔记三
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • %check_box% in rails :coditions={:has_many , :through}
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (C++20) consteval立即函数
  • (delphi11最新学习资料) Object Pascal 学习笔记---第14章泛型第2节(泛型类的类构造函数)
  • (ibm)Java 语言的 XPath API
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (void) (_x == _y)的作用
  • (二)丶RabbitMQ的六大核心
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (转)Oracle存储过程编写经验和优化措施
  • .aanva
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET CORE 第一节 创建基本的 asp.net core
  • .net 调用海康SDK以及常见的坑解释
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .Net(C#)常用转换byte转uint32、byte转float等