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

org.apache.commons.lang3都有什么常用的类

org.apache.commons.lang3是 Apache Commons Lang 库的核心包,提供了很多实用的工具类。以下是常用的类及示例说明:

1. ​StringUtils​ - 字符串工具类

// 空值安全处理
String str = null;
StringUtils.isEmpty(str);        // true
StringUtils.isBlank("  ");       // true
StringUtils.defaultIfBlank(str, "default"); // "default"// 字符串操作
StringUtils.substring("hello", 1, 3);     // "el"
StringUtils.join(new String[]{"a", "b"}, ","); // "a,b"
StringUtils.repeat("ab", 2);     // "abab"

2. ​ArrayUtils​ - 数组工具类

// 数组操作
int[] arr = {1, 2, 3};
int[] newArr = ArrayUtils.add(arr, 4);    // [1,2,3,4]
boolean contains = ArrayUtils.contains(arr, 2); // true
int[] empty = ArrayUtils.EMPTY_INT_ARRAY; // 空数组

3. ​DateUtils​ - 日期工具类

// 日期计算
Date now = new Date();
Date tomorrow = DateUtils.addDays(now, 1);
Date sameDayNextMonth = DateUtils.addMonths(now, 1);// 日期比较(忽略时间部分)
boolean sameDay = DateUtils.isSameDay(date1, date2);

4. ​NumberUtils​ - 数字工具类

// 数字转换和比较
NumberUtils.toInt("123", 0);     // 123,转换失败返回默认值0
NumberUtils.max(1, 3, 2);        // 3
NumberUtils.isDigits("123");     // true

5. ​BooleanUtils​ - 布尔工具类

// 布尔值转换
BooleanUtils.toBooleanObject("yes");     // Boolean.TRUE
BooleanUtils.negate(true);               // false
BooleanUtils.toStringOnOff(true);        // "on"

6. ​ObjectUtils​ - 对象工具类

// 空值安全处理
String result = ObjectUtils.defaultIfNull(str, "default");
ObjectUtils.firstNonNull(null, null, "value"); // "value"

7. ​RandomStringUtils​ - 随机字符串生成

// 生成随机字符串
RandomStringUtils.randomAlphabetic(10);  // 10位字母
RandomStringUtils.randomNumeric(6);      // 6位数字
RandomStringUtils.randomAlphanumeric(8); // 8位字母数字

8. ​Validate​ - 参数验证

// 参数校验
Validate.notNull(obj, "对象不能为空");
Validate.isTrue(age > 0, "年龄必须大于0: %d", age);
Validate.notEmpty(collection, "集合不能为空");

9. ​SystemUtils​ - 系统工具

// 系统信息
SystemUtils.IS_OS_WINDOWS;      // 是否Windows系统
SystemUtils.JAVA_HOME;          // Java安装目录
SystemUtils.USER_HOME;          // 用户主目录

10. ​ToStringBuilder​ - 对象toString

public class Person {private String name;private int age;@Overridepublic String toString() {return new ToStringBuilder(this).append("name", name).append("age", age).toString();}
}

11. ​EqualsBuilder/HashCodeBuilder

// 重写equals和hashCode
@Override
public boolean equals(Object obj) {return EqualsBuilder.reflectionEquals(this, obj);
}@Override
public int hashCode() {return HashCodeBuilder.reflectionHashCode(this);
}

12. ​StopWatch​ - 计时器

StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 执行任务...
stopWatch.stop();
System.out.println("耗时: " + stopWatch.getTime() + "ms");

这些工具类大大提高了开发效率,减少了重复代码的编写,是Java开发中非常实用的辅助工具。

http://www.dtcms.com/a/554185.html

相关文章:

  • edas会议投稿显示格式错误+消除浮动块下面的空白
  • 宁波建设网站公司北京seo案例
  • 虚拟网站仿制教程河南国控建设集团招标网站
  • viewerjs+vue3 using typescript
  • U81904 【模板】树的直径
  • 如何将React自定义语法转化为标准JavaScript语法?
  • 自己做网站主机wordpress 引号被转义
  • 做营销网站推广快速开发安卓app
  • 文件基础操作详解
  • 【22】C语言 - 二维数组详解
  • 嵌入式项目代码架构与分层笔记
  • 自己房子做民宿挂什么网站数字今天科技 网站
  • 建设ca网站aws wordpress 集群
  • Rust数据类型(上):标量类型全解析
  • BPC EPM表单常规设置
  • 关于C++递归函数和指针部分
  • 基于STM32的智能天气时钟
  • 传奇网站建设网站开发公用头部
  • 安徽省建设厅官方网站黄世山电商办公室
  • 做的网站上更改内容改怎么办科技公司logo设计图片
  • 飞腾D2000/8在Ubuntu20.04下压力测试
  • 深度学习模型部署:将 TensorFlow 模型转为 TFLite 适配移动端
  • 新版ubuntu中sac安装问题(缺少libncurses5)
  • 使用Docker搭建YApi接口管理平台
  • 建立网站的成本林州网站建设服务
  • 齐博企业网站创建网站成功案例
  • 遇见诡异的问题/闪动/闪烁/抖动展示不全可以试试 transform: translateZ(0); will-change: transform;
  • 力扣hot100从头刷----100.1环形链表
  • 吴镇宇做的电影教学网站做网站的服务器有什么作用
  • 如何将插入(insert)的记录id返回?