【2021Java最新学习路线,Hutool中那些常用的工具类和方法

这篇博客详细介绍了Java中常用的工具类库的使用,包括反射工具ReflectUtil、数字处理工具NumberUtil、集合操作工具CollUtil和MapUtil,以及安全加密工具SecureUtil。通过实例展示了如何进行对象方法获取、数字运算、集合转换以及MD5加密,帮助开发者提升代码效率。
摘要由CSDN通过智能技术生成

//格式化字符串
String template = “这只是个占位符:{}”;
String str2 = StrUtil.format(template, “我是占位符”);
LOGGER.info("/strUtil format:{}", str2);


# ClassPathResource

获取classPath下的文件,在Tomcat等容器下,classPath一般是WEB-INF/classes。

```java
//获取定义在src/main/resources文件夹中的配置文件
ClassPathResource resource = new ClassPathResource("generator.properties");
Properties properties = new Properties();
properties.load(resource.getStream());
LOGGER.info("/classPath:{}", properties);

ReflectUtil

Java反射工具类,可用于反射获取类的方法及创建对象。

//获取某个类的所有方法
Method[] methods = ReflectUtil.getMethods(PmsBrand.class);
//获取某个类的指定方法
Method method = ReflectUtil.getMethod(PmsBrand.class, "getId");
//使用反射来创建对象
PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);
//反射执行对象的方法
ReflectUtil.invoke(pmsBrand, "setId", 1);

NumberUtil

数字处理工具类,可用于各种类型数字的加减乘除操作及判断类型。

double n1 = 1.234;
double n2 = 1.234;
double result;
//对float、double、BigDecimal做加减乘除操作
result = NumberUtil.add(n1, n2);
result = NumberUtil.sub(n1, n2);
result = NumberUtil.mul(n1, n2);
result = NumberUtil.div(n1, n2);
//保留两位小数
BigDecimal roundNum = NumberUtil.round(n1, 2);
String n3 = "1.234";
//判断是否为数字、整数、浮点数
NumberUtil.isNumber(n3);
NumberUtil.isInteger(n3);
NumberUtil.isDouble(n3);

BeanUtil

JavaBean的工具类,可用于Map与JavaBean对象的互相转换以及对象属性的拷贝。

PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName("小米");
brand.setShowStatus(0);
//Bean转Map
Map<String, Object> map = BeanUtil.beanToMap(brand);
LOGGER.info("beanUtil bean to map:{}", map);
//Map转Bean
PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
LOGGER.info("beanUtil map to bean:{}", mapBrand);
//Bean属性拷贝
PmsBrand copyBrand = new PmsBrand();
BeanUtil.copyProperties(brand, copyBrand);
LOGGER.info("beanUtil copy properties:{}", copyBrand);

CollUtil

集合操作的工具类,定义了一些常用的集合操作。

//数组转换为列表
String[] array = new String[]{"a", "b", "c", "d", "e"};
List<String> list = CollUtil.newArrayList(array);
//join:数组转字符串时添加连接符号
String joinStr = CollUtil.join(list, ",");
LOGGER.info("collUtil join:{}", joinStr);
//将以连接符号分隔的字符串再转换为列表
List<String> splitList = StrUtil.split(joinStr, ',');
LOGGER.info("collUtil split:{}", splitList);
//创建新的Map、Set、List
HashMap<Object, Object> newMap = CollUtil.newHashMap();
HashSet<Object> newHashSet = CollUtil.newHashSet();
ArrayList<Object> newList = CollUtil.newArrayList();
//判断列表是否为空
CollUtil.isEmpty(list);

MapUtil

Map操作工具类,可用于创建Map对象及判断Map是否为空。

//将多个键值对加入到Map中
Map<Object, Object> map = MapUtil.of(new String[][]{
    {"key1", "value1"},
    {"key2", "value2"},
    {"key3", "value3"}
});
//判断Map是否为空
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);

AnnotationUtil

注解工具类,可用于获取注解与注解中指定的值。

//获取指定类、方法、字段、构造器上的注解列表
Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class, false);
LOGGER.info("annotationUtil annotations:{}", annotationList);
//获取指定类型注解
Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
LOGGER.info("annotationUtil api value:{}", api.description());
//获取指定类型注解的值
Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);

SecureUtil

加密解密工具类,可用于MD5加密。

//MD5加密
String str = "123456";
String md5Str = SecureUtil.md5(str);
LOGGER.info("secureUtil md5:{}", md5Str);

CaptchaUtil

验证码工具类,可用于生成图形验证码。



#### 线程、数据库、算法、JVM、分布式、微服务、框架、Spring相关知识

![](https://img-blog.csdnimg.cn/img_convert/d72c5fcd551bed3dcc88aa90997ce2f4.png)

#### 一线互联网P7面试集锦+各种大厂面试集锦

![](https://img-blog.csdnimg.cn/img_convert/979e1218d00e54e716cd2c4463b2912e.png)

**[资料领取方式:戳这里](https://gitee.com/vip204888/java-p7)**

#### 学习笔记以及面试真题解析

65996)]

#### 一线互联网P7面试集锦+各种大厂面试集锦

[外链图片转存中...(img-tab9OdF2-1628306265999)]

**[资料领取方式:戳这里](https://gitee.com/vip204888/java-p7)**

#### 学习笔记以及面试真题解析

![](https://img-blog.csdnimg.cn/img_convert/d662e1552f020b07de57f811efe1f0f9.png)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值