java开发最实用的工具类【建议收藏】

工具类

1、Collections

首先出场的是 java.util 包下的 Collections 类,该类主要用于操作集合或者返回集合,我个人非常喜欢用它。

① 排序

在工作中经常有对集合排序的需求。

看看使用 Collections 工具是如何实现升序和降序的:

  List<Integer> list = new ArrayList<>();
  list.add(2);
  list.add(1);
  list.add(3);
  // 升序
  Collections.sort(list);
  System.out.println(list);
  // 降序
  Collections.reverse(list);
  System.out.println(list);

  
  
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

执行结果:

[1, 2, 3]
[3, 2, 1]

  
  
    
    
    
    
  • 1
  • 2

② 获取最大或最小值

有时候需要找出集合中的最大值或者最小值,这时可以使用 Collectionsmaxmin 方法。例如:

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
Integer max = Collections.max(list);//获取最大值
Integer min = Collections.min(list);//获取最小值
System.out.println(max);
System.out.println(min);

  
  
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

执行结果:

3
1

  
  
    
    
    
    
  • 1
  • 2

③ 转换线程安全集合

我们都知道,java 中的很多集合,比如:ArrayListLinkedListHashMapHashSet等,都是线程不安全的。

换句话说,这些集合在多线程的环境中,添加数据会出现异常。

这时,可以用 Collectionssynchronizedxxx 方法,将这些线程不安全的集合,直接转换成线程安全集合。例如:

 List<Integer> list = new ArrayList<>();
  list.add(2);
  list.add(1);
  list.add(3);

// 将ArrayList转换成线程安全集合
List<Integer> integers = Collections.synchronizedList(list);
System.out.println(integers);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

它的底层会创建 SynchronizedRandomAccessList 或者 SynchronizedList 类,这两个类的很多方法都会用 synchronized 加锁。

④ 返回空集合

有时,我们在判空之后,需要返回空集合,就可以使用 emptyList 方法,例如:

private List<Integer> fun(List<Integer> list) {
    if (list == null || list.size() == 0) {
        return Collections.emptyList();
    }
    //业务处理
    return list;
}

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

⑤ 二分查找

binarySearch 方法提供了一个非常好用的二分查找功能,只用传入指定集合和需要找到的key 即可。例如:

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

// 二分查找
int i = Collections.binarySearch(list, 3);
System.out.println(i );

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

执行结果:

2

 
 
   
   
   
   
  • 1

⑥ 转换成不可修改集合

为了防止后续的程序把某个集合的结果修改了,有时候我们需要把某个集合定义成不可修改的,使用 Collectionsunmodifiablexxx 方法就能轻松实现:

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

List<Integer> integers = Collections.unmodifiableList(list);
integers.add(4);
System.out.println(integers);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

执行结果:

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
 at com.sue.jump.service.test1.UtilTest.main(UtilTest.java:19)

 
 
   
   
   
   
  • 1
  • 2
  • 3

当然 Collections 工具类中还有很多常用的方法,在这里就不一一介绍了,需要你自己去探索。

java开发最实用的工具类【建议收藏】_第1张图片
java开发最实用的工具类【建议收藏】_第2张图片

2、CollectionUtils

对集合操作,除了前面说的 Collections 工具类之后,CollectionUtils 工具类也非常常用。

目前比较主流的是 springorg.springframework.util 包下的 CollectionUtils 工具类。

java开发最实用的工具类【建议收藏】_第3张图片

apacheorg.apache.commons.collections 包下的 CollectionUtils 工具类。

java开发最实用的工具类【建议收藏】_第4张图片
java开发最实用的工具类【建议收藏】_第5张图片

我个人更推荐使用 apache 的包下的 CollectionUtils 工具类,因为它的工具更多更全面。

举个简单的例子,springCollectionUtils 工具类没有判断集合不为空的方法。而 apacheCollectionUtils 工具类却有。

下面我们以 apacheCollectionUtils 工具类为例,介绍一下常用方法。

① 集合判空

通过 CollectionUtils 工具类的 isEmpty 方法可以轻松判断集合是否为空,isNotEmpty 方法判断集合不为空。

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

if (CollectionUtils.isEmpty(list)) {
System.out.println(“集合为空”);
}

if (CollectionUtils.isNotEmpty(list)) {
System.out.println(“集合不为空”);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

② 对两个集合进行操作

有时候我们需要对已有的两个集合进行操作,比如取交集或者并集等。

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

List<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(4);

//获取并集
Collection<Integer> unionList = CollectionUtils.union(list, list2);
System.out.println(unionList);

//获取交集
Collection<Integer> intersectionList = CollectionUtils.intersection(list, list2);
System.out.println(intersectionList);

//获取交集的补集
Collection<Integer> disjunctionList = CollectionUtils.disjunction(list, list2);
System.out.println(disjunctionList);

//获取差集
Collection<Integer> subtractList = CollectionUtils.subtract(list, list2);
System.out.println(subtractList);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

执行结果:

[1, 2, 3, 4]
[2]
[1, 3, 4]
[1, 3]

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4

说句实话,对两个集合的操作,在实际工作中用得挺多的,特别是很多批量的场景中。以前我们需要写一堆代码,但没想到有现成的轮子。

3、Lists

如果你引入 com.google.guavapom 文件,会获得很多好用的小工具。这里推荐一款 com.google.common.collect 包下的集合工具:Lists

它是在太好用了,让我爱不释手。

① 创建空集合

有时候,我们想创建一个空集合。这时可以用 ListsnewArrayList 方法,例如:

List<Integer> list = Lists.newArrayList();

 
 
   
   
   
   
  • 1

② 快速初始化集合

有时候,我们想给一个集合中初始化一些元素。这时可以用 ListsnewArrayList 方法,例如:

List<Integer> list = Lists.newArrayList(1, 2, 3);

 
 
   
   
   
   
  • 1

执行结果:

[1, 2, 3]

 
 
   
   
   
   
  • 1

③ 笛卡尔积

如果你想将两个集合做 笛卡尔积ListscartesianProduct 方法可以帮你实现:

List<Integer> list1 = Lists.newArrayList(1, 2, 3);
List<Integer> list2 = Lists.newArrayList(4,5);
List<List<Integer>> productList = Lists.cartesianProduct(list1,list2);
System.out.println(productList);

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4

执行结果:

[[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

 
 
   
   
   
   
  • 1

④ 分页

如果你想将一个 大集合 分成若干个 小集合,可以使用 Listspartition 方法:

List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
List<List<Integer>> partitionList = Lists.partition(list, 2);
System.out.println(partitionList);

 
 
   
   
   
   
  • 1
  • 2
  • 3

执行结果:

[[1, 2], [3, 4], [5]]

 
 
   
   
   
   
  • 1

这个例子中,list 有 5 条数据,我将 list 集合按大小为 2,分成了 3 页,即变成 3 个小集合。

这个是我最喜欢的方法之一,经常在项目中使用。

比如有个需求:现在有 5000 个 id,需要调用批量用户查询接口,查出用户数据。但如果你直接查 5000 个用户,单次接口响应时间可能会非常慢。如果改成分页处理,每次只查 500 个用户,异步调用 10 次接口,就不会有单次接口响应慢的问题。

⑤ 流处理

如果我们想把某个集合转换成另外一个接口,可以使用 Liststransform 方法。例如:

List<String> list = Lists.newArrayList("a","b","c");
List<String> transformList = Lists.transform(list, x -> x.toUpperCase());
System.out.println(transformList);

 
 
   
   
   
   
  • 1
  • 2
  • 3

将小写字母转换成了大写字母。

⑥ 颠倒顺序

Lists 的有颠倒顺序的方法 reverse。例如:

List<Integer> list = Lists.newArrayList(3, 1, 2);
List<Integer> reverseList = Lists.reverse(list);
System.out.println(reverseList);

 
 
   
   
   
   
  • 1
  • 2
  • 3

执行结果:

[2, 1, 3]

 
 
   
   
   
   
  • 1

list 的原始顺序是 312,使用 reverse 方法颠倒顺序之后,变成了 213。

Lists 还有其他的好用的工具,我在这里只是抛砖引玉,有兴趣的朋友,可以仔细研究一下。

java开发最实用的工具类【建议收藏】_第6张图片

4、Objects

jdk7 之后,提供了 Objects 工具类,我们可以通过它操作对象。

① 对象判空

java 中万事万物皆对象,对象的判空可以说无处不在。ObjectsisNull 方法判断对象是否为空,而 nonNull 方法判断对象是否不为空。例如:

Integer integer = new Integer(1);

if (Objects.isNull(integer)) {
System.out.println(“对象为空”);
}

if (Objects.nonNull(integer)) {
System.out.println(“对象不为空”);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

② 对象为空抛异常

如果我们想在对象为空时,抛出空指针异常,可以使用 ObjectsrequireNonNull 方法。例如:

Integer integer1 = new Integer(128);

Objects.requireNonNull(integer1);
Objects.requireNonNull(integer1, “参数不能为空”);
Objects.requireNonNull(integer1, () -> “参数不能为空”);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

③ 判断两个对象是否相等

我们经常需要判断两个对象是否相等,Objects 给我们提供了 equals 方法,能非常方便的实现:

Integer integer1 = new Integer(1);
Integer integer2 = new Integer(1);

System.out.println(Objects.equals(integer1, integer2));

  • 1
  • 2
  • 3
  • 4

执行结果:

true

 
 
   
   
   
   
  • 1

④ 获取对象的 hashCode

如果你想获取某个对象的 hashCode,可以使用 ObjectshashCode 方法。例如:

String str = new String("abc");
System.out.println(Objects.hashCode(str));

 
 
   
   
   
   
  • 1
  • 2

执行结果:

96354

 
 
   
   
   
   
  • 1

Objects 的内容先介绍到这里,有兴趣的小伙们,可以看看下面更多的方法:

java开发最实用的工具类【建议收藏】_第7张图片

5、BooleanUtils

java 中布尔值,随处可见。

如果你使用了布尔的包装类:Boolean,总感觉有点麻烦,因为它有三种值:nulltruefalse。我们在处理 Boolean 对象时,需要经常判空。

头疼!!!

但如果使用 BooleanUtils 类处理布尔值,心情一下子就愉悦起来了。

① 判断 true 或 false

如果你想判断某个参数的值是 truefalse,可以直接使用 isTrueisFalse 方法。例如:

Boolean aBoolean = new Boolean(true);
System.out.println(BooleanUtils.isTrue(aBoolean));
System.out.println(BooleanUtils.isFalse(aBoolean));

 
 
   
   
   
   
  • 1
  • 2
  • 3

② 判断不为true或不为false

有时候,需要判断某个参数不为 true,即是 null 或者 false。或者判断不为 false,即是 null 或者 true

可以使用 isNotTrueisNotFalse 方法。例如:

Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = null;
System.out.println(BooleanUtils.isNotTrue(aBoolean));
System.out.println(BooleanUtils.isNotTrue(aBoolean1));
System.out.println(BooleanUtils.isNotFalse(aBoolean));
System.out.println(BooleanUtils.isNotFalse(aBoolean1));

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

执行结果:

false
true
true
true

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4

③ 转换成数字

如果你想将 true 转换成数字 1,false 转换成数字 0,可以使用 toInteger 方法:

Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = new Boolean(false);
System.out.println(BooleanUtils.toInteger(aBoolean));
System.out.println(BooleanUtils.toInteger(aBoolean1));

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4

执行结果:

1
0

 
 
   
   
   
   
  • 1
  • 2

④ Boolean转换成布尔值

我们有时候需要将包装类 Boolean 对象,转换成原始的 boolean 对象,可以使用 toBoolean 方法。例如:

Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = null;
System.out.println(BooleanUtils.toBoolean(aBoolean));
System.out.println(BooleanUtils.toBoolean(aBoolean1));
System.out.println(BooleanUtils.toBooleanDefaultIfNull(aBoolean1, false));

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

我们无需额外的判空了,而且还可以设置 Boolean 对象为空时返回的默认值。

BooleanUtils 类的方法还有很多,有兴趣的小伙伴可以看看下面的内容:

java开发最实用的工具类【建议收藏】_第8张图片

6、StringUtils

字符串(String)在我们的日常工作中,用得非常非常非常多。

在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。

如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。

现在有个好消息是:org.apache.commons.lang3 包下的 StringUtils 工具类,给我们提供了非常丰富的选择。

① 字符串判空

其实空字符串,不只是 null 一种,还有 “”," ",“null” 等等,多种情况。

StringUtils 给我们提供了多个判空的静态方法,例如:

String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "abc";
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isEmpty(str2));
System.out.println(StringUtils.isEmpty(str3));
System.out.println(StringUtils.isEmpty(str4));
System.out.println("=====");
System.out.println(StringUtils.isNotEmpty(str1));
System.out.println(StringUtils.isNotEmpty(str2));
System.out.println(StringUtils.isNotEmpty(str3));
System.out.println(StringUtils.isNotEmpty(str4));
System.out.println("=====");
System.out.println(StringUtils.isBlank(str1));
System.out.println(StringUtils.isBlank(str2));
System.out.println(StringUtils.isBlank(str3));
System.out.println(StringUtils.isBlank(str4));
System.out.println("=====");
System.out.println(StringUtils.isNotBlank(str1));
System.out.println(StringUtils.isNotBlank(str2));
System.out.println(StringUtils.isNotBlank(str3));
System.out.println(StringUtils.isNotBlank(str4));

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

执行结果:


true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

示例中的:isEmptyisNotEmptyisBlankisNotBlank,这 4 个判空方法你们可以根据实际情况使用。

优先推荐使用 isBlankisNotBlank 方法,因为它会把 " " 也考虑进去。

② 分隔字符串

分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。

String str1 = null;
System.out.println(StringUtils.split(str1,","));
System.out.println(str1.split(","));

 
 
   
   
   
   
  • 1
  • 2
  • 3

执行结果:

null
Exception in thread "main" java.lang.NullPointerException
 at com.sue.jump.service.test1.UtilTest.main(UtilTest.java:21)

 
 
   
   
   
   
  • 1
  • 2
  • 3

使用 StringUtilssplit 方法会返回 null,而使用 Stringsplit 方法会报指针异常。

③ 判断是否纯数字

给定一个字符串,判断它是否为纯数字,可以使用isNumeric 方法。例如:

String str1 = "123";
String str2 = "123q";
String str3 = "0.33";
System.out.println(StringUtils.isNumeric(str1));
System.out.println(StringUtils.isNumeric(str2));
System.out.println(StringUtils.isNumeric(str3));

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

执行结果:

true
false
false

 
 
   
   
   
   
  • 1
  • 2
  • 3

④ 将集合拼接成字符串

有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用 join 方法。例如:

List<String> list = Lists.newArrayList("a", "b", "c");
List<Integer> list2 = Lists.newArrayList(1, 2, 3);
System.out.println(StringUtils.join(list, ","));
System.out.println(StringUtils.join(list2, " "));

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4

执行结果:

a,b,c
1 2 3

 
 
   
   
   
   
  • 1
  • 2

当然还有很多实用的方法,我在这里就不一一介绍了。

java开发最实用的工具类【建议收藏】_第9张图片
java开发最实用的工具类【建议收藏】_第10张图片

7、Assert

很多时候,我们需要在代码中做判断:如果不满足条件,则抛异常。

有没有统一的封装呢?

其实 spring 给我们提供了 Assert 类,它表示断言。

① 断言参数是否为空

断言 参数是否空,如果不满足条件,则直接抛异常。

String str = null;
Assert.isNull(str, "str必须为空");
Assert.isNull(str, () -> "str必须为空");
Assert.notNull(str, "str不能为空");

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4

如果不满足条件就会抛出 IllegalArgumentException 异常。

② 断言集合是否为空

断言 集合是否空,如果不满足条件,则直接抛异常。

List<String> list = null;
Map<String, String> map = null;
Assert.notEmpty(list, "list不能为空");
Assert.notEmpty(list, () -> "list不能为空");
Assert.notEmpty(map, "map不能为空");

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

如果不满足条件就会抛出 IllegalArgumentException 异常。

③ 断言条件是否为空

断言 是否满足某个条件,如果不满足条件,则直接抛异常。

List<String> list = null;
Assert.isTrue(CollectionUtils.isNotEmpty(list), "list不能为空");
Assert.isTrue(CollectionUtils.isNotEmpty(list), () -> "list不能为空");

 
 
   
   
   
   
  • 1
  • 2
  • 3

当然 Assert 类还有一些其他的功能,这里就不多介绍了。

java开发最实用的工具类【建议收藏】_第11张图片

8、IOUtils

IO 流在我们日常工作中也用得比较多,尽管 java 已经给我们提供了丰富的 API

但我们不得不每次读取文件,或者写入文件之后,写一些重复的的代码。手动在 finally 代码块中关闭流,不然可能会造成内存溢出。

有个好消息是:如果你使用 org.apache.commons.io 包下的 IOUtils 类,会节省大量的时间。

① 读取文件

如果你想将某个 txt 文件中的数据,读取到字符串当中,可以使用 IOUtils 类的 toString 方法。例如:

String str = IOUtils.toString(new FileInputStream("/temp/a.txt"), StandardCharsets.UTF_8);
System.out.println(str);

 
 
   
   
   
   
  • 1
  • 2

② 写入文件

如果你想将某个字符串的内容,写入到指定文件当中,可以使用 IOUtils 类的 write 方法。例如:

String str = "abcde";
IOUtils.write(str, new FileOutputStream("/temp/b.tx"), StandardCharsets.UTF_8);

 
 
   
   
   
   
  • 1
  • 2

③ 文件拷贝

如果你想将某个文件中的所有内容,都拷贝到另一个文件当中,可以使用 IOUtils 类的 copy 方法。例如:

IOUtils.copy(new FileInputStream("/temp/a.txt"), new FileOutputStream("/temp/b.txt"));

 
 
   
   
   
   
  • 1

④ 读取文件内容到字节数组

如果你想将某个文件中的内容,读取字节数组中,可以使用 IOUtils 类的 toByteArray 方法。例如:

byte[] bytes = IOUtils.toByteArray(new FileInputStream("/temp/a.txt"));

 
 
   
   
   
   
  • 1

java开发最实用的工具类【建议收藏】_第12张图片

9、ClassUtils

springorg.springframework.util 包下的 ClassUtils 类,它里面有很多让我们惊喜的功能。

它里面包含了类和对象相关的很多非常实用的方法。

① 获取对象的所有接口

如果你想获取某个对象的所有接口,可以使用 ClassUtilsgetAllInterfaces 方法。例如:


Class<?>[] allInterfaces = ClassUtils.getAllInterfaces(new User());

 
 
   
   
   
   
  • 1
  • 2

② 获取某个类的包名

如果你想获取某个类的包名,可以使用 ClassUtilsgetPackageName 方法。例如:

String packageName = ClassUtils.getPackageName(User.class);
System.out.println(packageName);

 
 
   
   
   
   
  • 1
  • 2

③ 判断某个类是否内部类

如果你想判断某个类是否内部类,可以使用 ClassUtilsisInnerClass 方法。例如:

System.out.println(ClassUtils.isInnerClass(User.class));

 
 
   
   
   
   
  • 1

④ 判断对象是否代理对象

如果你想判断对象是否代理对象,可以使用 ClassUtilsisCglibProxy 方法。例如:

System.out.println(ClassUtils.isCglibProxy(new User()));

 
 
   
   
   
   
  • 1

ClassUtils 还有很多有用的方法,等待着你去发掘。感兴趣的朋友,可以看看下面内容:

java开发最实用的工具类【建议收藏】_第13张图片

10、BeanUtils

spring 给我们提供了一个 JavaBean 的工具类,它在 org.springframework.beans 包下面,它的名字叫做:BeanUtils

让我们一起看看这个工具可以带给我们哪些惊喜。

① 拷贝对象的属性

曾几何时,你有没有这样的需求:把某个对象中的所有属性,都拷贝到另外一个对象中。这时就能使用 BeanUtilscopyProperties 方法。例如:

User user1 = new User();
user1.setId(1L);
user1.setName("张三");
user1.setAddress("成都");

User user2 = new User();
BeanUtils.copyProperties(user1, user2);
System.out.println(user2);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

② 实例化某个类

如果你想通过反射实例化一个类的对象,可以使用 BeanUtilsinstantiateClass 方法。例如:

User user = BeanUtils.instantiateClass(User.class);
System.out.println(user);

 
 
   
   
   
   
  • 1
  • 2

③ 获取指定类的指定方法

如果你想获取某个类的指定方法,可以使用 BeanUtilsfindDeclaredMethod 方法。例如:

Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
System.out.println(declaredMethod.getName());

 
 
   
   
   
   
  • 1
  • 2

④ 获取指定方法的参数

如果你想获取某个方法的参数,可以使用 BeanUtilsfindPropertyForMethod 方法。例如:

Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
PropertyDescriptor propertyForMethod = BeanUtils.findPropertyForMethod(declaredMethod);
System.out.println(propertyForMethod.getName());

 
 
   
   
   
   
  • 1
  • 2
  • 3

java开发最实用的工具类【建议收藏】_第14张图片

11、ReflectionUtils

有时候,我们需要在项目中使用反射功能,如果使用最原始的方法来开发,代码量会非常多,而且很麻烦,它需要处理一大堆异常以及访问权限等问题。

好消息是 spring 给我们提供了一个 ReflectionUtils 工具,它在 org.springframework.util 包下面。

① 获取方法

如果你想获取某个类的某个方法,可以使用 ReflectionUtils 类的 findMethod 方法。例如:

Method method = ReflectionUtils.findMethod(User.class, "getId");

 
 
   
   
   
   
  • 1

② 获取字段

如果你想获取某个类的某个字段,可以使用 ReflectionUtils 类的 findField 方法。例如:

Field field = ReflectionUtils.findField(User.class, "id");

 
 
   
   
   
   
  • 1

③ 执行方法

如果你想通过反射调用某个方法,传递参数,可以使用 ReflectionUtils 类的 invokeMethod 方法。例如:

ReflectionUtils.invokeMethod(method, springContextsUtil.getBean(beanName), param);

 
 
   
   
   
   
  • 1

④ 判断字段是否常量

如果你想判断某个字段是否常量,可以使用 ReflectionUtils 类的 isPublicStaticFinal 方法。例如:

Field field = ReflectionUtils.findField(User.class, "id");
System.out.println(ReflectionUtils.isPublicStaticFinal(field));

 
 
   
   
   
   
  • 1
  • 2

⑤ 判断是否 equals 方法

如果你想判断某个方法是否 equals 方法,可以使用 ReflectionUtils 类的 isEqualsMethod 方法。例如:

Method method = ReflectionUtils.findMethod(User.class, "getId");
System.out.println(ReflectionUtils.isEqualsMethod(method));

 
 
   
   
   
   
  • 1
  • 2

当然这个类还有不少有趣的方法,感兴趣的朋友,可以看看下面内容:

java开发最实用的工具类【建议收藏】_第15张图片

12、Base64Utils

有时候,为了安全考虑,需要将参数值用 base64 编码。

这时就能直接使用 org.springframework.util 包下的 Base64Utils 工具类。

它里面包含:encodedecode 方法,用于对数据进行加密和解密。例如:

String str = "abc";
String encode = new String(Base64Utils.encode(str.getBytes()));
System.out.println("加密后:" + encode);
try {
    String decode = new String(Base64Utils.decode(encode.getBytes()), "utf8");
    System.out.println("解密后:" + decode);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

执行结果:

加密后:YWJj
解密后:abc

 
 
   
   
   
   
  • 1
  • 2

13、StandardCharsets

我们在做字符转换的时候,经常需要指定字符编码,比如:UTF-8ISO-8859-1 等等。

这时就可以直接使用 java.nio.charset 包下的 StandardCharsets 类中静态变量。

例如:


String str = "abc";
String encode = new String(Base64Utils.encode(str.getBytes()));
System.out.println("加密后:" + encode);
String decode = new String(Base64Utils.decode(encode.getBytes())
, StandardCharsets.UTF_8);
System.out.println("解密后:" + decode);

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

14、DigestUtils

有时候,我们需要对数据进行加密处理,比如:md5sha256

可以使用 apacheorg.apache.commons.codec.digest 包下的 DigestUtils 类。

① md5 加密

如果你想对数据进行 md5 加密,可以使用 DigestUtilsmd5Hex 方法。例如:

String md5Hex = DigestUtils.md5Hex("加密");
System.out.println(md5Hex);

 
 
   
   
   
   
  • 1
  • 2

② sha256 加密

如果你想对数据进行 sha256 加密,可以使用 DigestUtilssha256Hex 方法。例如:

String md5Hex = DigestUtils.sha256Hex("加密");
System.out.println(md5Hex);

 
 
   
   
   
   
  • 1
  • 2

当然这个工具还有很多其他的加密方法:

java开发最实用的工具类【建议收藏】_第16张图片

15、SerializationUtils

有时候,我们需要把数据进行序列化和反序列化处理。

传统的做法是某个类实现 Serializable 接口,然后重新它的 writeObjectreadObject 方法。

但如果使用 org.springframework.util 包下的 SerializationUtils 工具类,能更轻松实现序列化和反序列化功能。例如:


Map<String, String> map = Maps.newHashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
byte[] serialize = SerializationUtils.serialize(map);
Object deserialize = SerializationUtils.deserialize(serialize);
System.out.println(deserialize);

 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

16、HttpStatus

很多时候,我们会在代码中定义http的返回码,比如:接口正常返回 200,异常返回 500,接口找不到返回 404,接口不可用返回 502 等。

private int SUCCESS_CODE = 200;
private int ERROR_CODE = 500;
private int NOT_FOUND_CODE = 404;

 
 
   
   
   
   
  • 1
  • 2
  • 3

其实 org.springframework.http 包下的 HttpStatus 枚举,或者 org.apache.http 包下的 HttpStatus 接口,已经把常用的 http 返回码给我们定义好了,直接拿来用就可以了,真的不用再重复定义了。

java开发最实用的工具类【建议收藏】_第17张图片
转载:https://blog.csdn.net/Always206/article/details/125457453。

你可能感兴趣的:(日常积累,java,开发语言,intellij-idea)