Java开发中常用的工具类总结

Java开发中常用的工具类总结

方法总览表

工具类 描述
Collections 提供操作集合的静态方法,如排序、查找最大/最小值等。
CollectionUtils 提供额外的集合操作方法,如判空、交集、并集、差集等。
Lists (Guava) 提供便捷的列表操作方法,如快速初始化、分割、反转等。
Objects 提供对象操作方法,如判空、对象比较、抛出异常等。
StringUtils (Apache Commons Lang) 提供丰富的字符串操作方法,如判空、分割、拼接等。
BeanUtils (Spring) 简化Java Bean对象之间的属性拷贝操作。
ReflectionUtils (Spring) 简化反射操作,如通过反射调用方法、获取属性等。
DigestUtils (Apache Commons Codec) 提供数据加密方法,如MD5、SHA256加密。
HttpStatus (Spring) 提供HTTP状态码枚举,方便处理HTTP响应。

导入和Maven依赖

import com.google.common.collect.Lists;
import com.one_tree.idea_demo.jdk.People;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.ReflectionUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.2</version>
</dependency>

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.2-jre</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.9</version>
</dependency>

一、Collections工具类

描述: 提供操作集合的静态方法,如排序、查找最大/最小值、创建不可修改集合等。

// 示例用法
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(6);
list.add(3);
list.add(5);

// 升序排序
Collections.sort(list);
System.out.println(list);

// 反转列表
Collections.reverse(list);
System.out.println(list);

// 查找最大值和最小值
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));

二、CollectionUtils工具类

描述: 提供额外的集合操作方法,如判空、交集、并集、差集等。

// 示例用法
// 判空
CollectionUtils.isEmpty(list);

List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(4);
list2.add(5);
list2.add(8);

// 获取并集
Collection<Integer> unionList = CollectionUtils.union(list, list2);
// 获取交集
Collection<Integer> intersectionList = CollectionUtils.intersection(list, list2);
// 获取差集
Collection<Integer> subtractList = CollectionUtils.subtract(list, list2);

三、Lists工具类 (Guava)

描述: 提供便捷的列表操作方法,如快速初始化、分割、反转等。

// 示例用法
// 快速初始化列表
List<Integer> gList = Lists.newArrayList(1, 2, 3);
// 分割列表
List<List<Integer>> partitionList = Lists.partition(list, 2);
// 反转列表
List<Integer> reverseList = Lists.reverse(list);

四、Objects工具类

描述: 提供对象操作方法,如判空、对象比较、抛出异常等。

// 示例用法
Integer i = new Integer(1);
// 判空
boolean result1 = Objects.isNull(i);
boolean result2 = Objects.nonNull(i);

// 对象比较
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(Objects.equals(i1, i2));

五、StringUtils工具类 (Apache Commons Lang)

描述: 提供丰富的字符串操作方法,如判空、分割、拼接等。

// 示例用法
String str1 = null;
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isNotEmpty(str1));

String str3 = "abcd,dd";
System.out.println(StringUtils.split(str3, ","));

List<String> strList1 = Lists.newArrayList("a", "b", "c");
System.out.println(StringUtils.join(strList1, ","));

六、BeanUtils工具类 (Spring)

描述: 简化Java Bean对象之间的属性拷贝操作。

// 示例用法
People people1 = new People();
people1.setAge(11);
people1.setName("一棵树");
People people2 = new People();
BeanUtils.copyProperties(people1, people2);
System.out.println(people2);

七、ReflectionUtils工具类 (Spring)

描述: 简化反射操作,如通过反射调用方法、获取属性等。

// 示例用法
Method method = ReflectionUtils.findMethod(People.class, "getAge");
Field field = ReflectionUtils.findField(People.class, "name");

People people = new People();
Method setMethod = ReflectionUtils.findMethod(People.class, "setAge");
ReflectionUtils.invokeMethod(setMethod, people, "12");

八、DigestUtils工具类 (Apache Commons Codec)

描述: 提供数据加密方法,如MD5、SHA256加密。

// 示例用法
String md5Hex = DigestUtils.md5Hex("Dylan");
String sha256Hex = DigestUtils.sha256Hex("Dylan");

九、HttpStatus工具类 (Spring)

描述: 提供HTTP状态码枚举,方便处理HTTP响应。

// 示例用法
int value503 = HttpStatus.SERVICE_UNAVAILABLE.value();
int success = HttpStatus.OK.value();
posted @ 2025-01-16 09:02  懒懒初阳  阅读(210)  评论(0)    收藏  举报