提交 939608a3 编写于 作者: 沉默王二's avatar 沉默王二 💬

常用工具类

上级 b618893e
......@@ -197,11 +197,12 @@
- [Java Scanner:扫描控制台输入的工具类](docs/common-tool/scanner.md)
- [Java Arrays:专为数组而生的工具类](docs/common-tool/arrays.md)
- [Apache StringUtils:专为Java字符串而生的工具类](docs/common-tool/StringUtils.md)
- [Objects:专为操作Java对象而生的工具类](docs/common-tool/Objects.md)
- [Java Collections:专为集合而生的工具类](docs/common-tool/collections.md)
- [Hutool:国产良心工具包,让你的Java变得更甜](docs/common-tool/hutool.md)
- [Guava:Google开源的Java工具库,太强大了](docs/common-tool/guava.md)
- [其他常用Java工具类:IpUtil、CollectionUtils、StringUtils、MDC、ClassUtils、BeanUtils、ReflectionUtils](docs/common-tool/utils.md)
- [其他常用Java工具类:IpUtil、MDC、ClassUtils、BeanUtils、ReflectionUtils](docs/common-tool/utils.md)
## Java新特性
......
......@@ -12,7 +12,7 @@ head:
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Objects,java objects
---
# 9.3 Objects
# 9.4 Objects
Java 的 Objects 类是一个实用工具类,包含了一系列静态方法,用于处理对象。它位于 java.util 包中,自 Java 7 引入。Objects 类的主要目的是降低代码中的[空指针异常](https://tobebetterjavaer.com/exception/npe.html) (NullPointerException) 风险,同时提供一些非常实用的方法供我们使用。
......
---
title: Apache StringUtils:专为Java字符串而生的工具类
shortTitle: Apache StringUtils
category:
- Java核心
tag:
- 常用工具类
description: Java程序员进阶之路,小白的零基础Java教程,从入门到进阶,Java Arrays工具类的10大常用方法
head:
- - meta
- name: keywords
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Apache StringUtils,java StringUtils
---
# 9.3 Apache StringUtils
`字符串`[String](https://tobebetterjavaer.com/string/immutable.html))在我们的日常工作中,用得非常非常非常多。
在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、[分隔字符串](https://tobebetterjavaer.com/string/split.html)[比较字符串](https://tobebetterjavaer.com/string/equals.html)、去掉多余空格、[拼接字符串](https://tobebetterjavaer.com/string/join.html)、使用正则表达式等等。
如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。
现在有个好消息是:`org.apache.commons.lang3`包下的`StringUtils`工具类,给我们提供了非常丰富的选择。
Maven 坐标:
```
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
```
StringUtils 提供了非常多实用的方法,大概有下图的四页到五页,我只截了两页,实在是太多了。
![](https://cdn.tobebetterjavaer.com/stutymore/StringUtils-20230330111122.png)
接下来,我们来拿一些常用的方法举例说明。
### 字符串判空
其实空字符串,不只是 null 一种,还有""," ","null"等等,多种情况。
StringUtils 给我们提供了多个判空的静态方法,例如:
```java
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));
```
执行结果:
```java
true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true
```
示例中的:`isEmpty``isNotEmpty``isBlank``isNotBlank`,这 4 个判空方法你们可以根据实际情况使用。
优先推荐使用`isBlank``isNotBlank`方法,因为它会把`" "`也考虑进去。
### 分隔字符串
分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。
```java
String str1 = null;
System.out.println(StringUtils.split(str1,","));
System.out.println(str1.split(","));
```
执行结果:
```java
null
Exception in thread "main" java.lang.NullPointerException
\tat com.sue.jump.service.test1.UtilTest.main(UtilTest.java:21)
```
使用 StringUtils 的 split 方法会返回 null,而使用 String 的 split 方法会报指针异常。
### 判断是否纯数字
给定一个字符串,判断它是否为纯数字,可以使用`isNumeric`方法。例如:
```java
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));
```
执行结果:
```java
true
false
false
```
### 将集合拼接成字符串
有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用`join`方法。例如:
```java
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, " "));
```
执行结果:
```java
a,b,c
1 2 3
```
### 其他方法
这里再列举一些,其他的方法可以自己去研究一下。
- `trim(String str)`:去除字符串首尾的空白字符。
- `trimToEmpty(String str)`:去除字符串首尾的空白字符,如果字符串为 null,则返回空字符串。
- `trimToNull(String str)`:去除字符串首尾的空白字符,如果结果为空字符串,则返回 null。
- `equals(String str1, String str2)`:比较两个字符串是否相等。
- `equalsIgnoreCase(String str1, String str2)`:比较两个字符串是否相等,忽略大小写。
- `startsWith(String str, String prefix)`:检查字符串是否以指定的前缀开头。
- `endsWith(String str, String suffix)`:检查字符串是否以指定的后缀结尾。
- `contains(String str, CharSequence seq)`:检查字符串是否包含指定的字符序列。
- `indexOf(String str, CharSequence seq)`:返回指定字符序列在字符串中首次出现的索引,如果没有找到,则返回 -1。
- `lastIndexOf(String str, CharSequence seq)`:返回指定字符序列在字符串中最后一次出现的索引,如果没有找到,则返回 -1。
- `substring(String str, int start, int end)`:截取字符串中指定范围的子串。
- `replace(String str, String searchString, String replacement)`:替换字符串中所有出现的搜索字符串为指定的替换字符串。
- `replaceAll(String str, String regex, String replacement)`:使用正则表达式替换字符串中所有匹配的部分。
- `join(Iterable<?> iterable, String separator)`:使用指定的分隔符将可迭代对象中的元素连接为一个字符串。
- `split(String str, String separator)`:使用指定的分隔符将字符串分割为一个字符串数组。
- `capitalize(String str)`:将字符串的第一个字符转换为大写。
- `uncapitalize(String str)`:将字符串的第一个字符转换为小写。
----
最近整理了一份牛逼的学习资料,包括但不限于Java基础部分(JVM、Java集合框架、多线程),还囊括了 **数据库、计算机网络、算法与数据结构、设计模式、框架类Spring、Netty、微服务(Dubbo,消息队列) 网关** 等等等等……详情戳:[可以说是2022年全网最全的学习和找工作的PDF资源了](https://tobebetterjavaer.com/pdf/programmer-111.html)
微信搜 **沉默王二** 或扫描下方二维码关注二哥的原创公众号沉默王二,回复 **111** 即可免费领取。
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/gongzhonghao.png)
\ No newline at end of file
......@@ -12,7 +12,7 @@ head:
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Collections,集合框架,java Collections
---
# 9.4 Collections
# 9.5 Collections
Collections 是 JDK 提供的一个工具类,位于 java.util 包下,提供了一系列的静态方法,方便我们对集合进行各种骚操作,算是集合框架的一个大管家。
......@@ -250,6 +250,93 @@ addAll 后:[沉默王九, 沉默王十, 沉默王二]
是否没有交集:否
```
### 06、CollectionUtils:Spring 和 Apache 都有提供的集合工具类
对集合操作,除了前面说的 JDK 原生 `Collections` 工具类,`CollectionUtils`工具类也很常用。
目前比较主流的是`Spring``org.springframework.util`包下的 CollectionUtils 工具类。
![](https://cdn.tobebetterjavaer.com/stutymore/utils-20230330101919.png)
`Apache``org.apache.commons.collections`包下的 CollectionUtils 工具类。
![](https://cdn.tobebetterjavaer.com/stutymore/utils-20230330103825.png)
Maven 坐标如下:
```
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
```
Apache 的方法比 Spring 的更多一些,我们就以 Apache 的为例,来介绍一下常用的方法。
#### 集合判空
通过 CollectionUtils 工具类的`isEmpty`方法可以轻松判断集合是否为空,`isNotEmpty`方法判断集合不为空。
```java
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("集合不为空");
}
```
#### 对两个集合进行操作
有时候我们需要对已有的两个集合进行操作,比如取交集或者并集等。
```java
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);
```
执行结果:
```java
[1, 2, 3, 4]
[2]
[1, 3, 4]
[1, 3]
```
说句实话,对两个集合的操作,在实际工作中用得挺多的,特别是很多批量的场景中。以前我们需要写一堆代码,但没想到有现成的轮子。
### 07、小结
整体上,Collections 工具类作为集合框架的大管家,提供了一些非常便利的方法供我们调用,也非常容易掌握,没什么难点,看看方法的注释就能大致明白干嘛的。
不过,工具就放在那里,用是一回事,为什么要这么用就是另外一回事了。能不能提高自己的编码水平,很大程度上取决于你到底有没有去钻一钻源码,看这些设计 JDK 的大师们是如何写代码的,学会一招半式,在工作当中还是能很快脱颖而出的。
......
......@@ -12,7 +12,7 @@ head:
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Guava,java guava,google guava
---
# 9.6 Guava
# 9.7 Guava
### 01、前世今生
......@@ -329,7 +329,7 @@ Lists还有其他的好用的工具,我在这里只是抛砖引玉,有兴趣
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/guava-4b962b06-a626-4707-9fe9-f5729536d9c5.jpg)
### 07、尾声
### 08、尾声
上面介绍了我认为最常用的功能,作为 Google 公司开源的 Java 开发核心库,个人觉得实用性还是很高的(不然呢?嘿嘿嘿)。引入到你的项目后不仅能快速的实现一些开发中常用的功能,而且还可以让代码更加的优雅简洁。
......
......@@ -12,7 +12,7 @@ head:
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Hutool,java hutool
---
# 9.5 Hutool
# 9.6 Hutool
读者群里有个小伙伴感慨说,“Hutool 这款开源类库太厉害了,基本上该有该的工具类,它里面都有。”讲真的,我平常工作中也经常用 Hutool,它确实可以帮助我们简化每一行代码,使 Java 拥有函数式语言般的优雅,让 Java 语言变得“甜甜的”。
......
---
title: 其他常用Java工具类:IpUtil、CollectionUtils、StringUtils、MDC、ClassUtils、BeanUtils、ReflectionUtils
title: 其他常用Java工具类:IPUtil、CollectionUtils、MDC、ClassUtils、BeanUtils、ReflectionUtils
shortTitle: 其他常用Java工具类
category:
- Java核心
......@@ -9,10 +9,10 @@ description: Java程序员进阶之路,小白的零基础Java教程,从入
head:
- - meta
- name: keywords
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,工具类,轮子,java 工具类
content: Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,工具类,轮子,java 工具类,java IPUtil,java CollectionUtils,
---
# 9.7 其他常用Java工具类
# 9.8 其他常用 Java 工具类
除了我们前面提到的 Java 原生工具类,比如说 [Arrays](https://tobebetterjavaer.com/common-tool/arrays.html)[Objects](https://tobebetterjavaer.com/common-tool/Objects.html)[Collections](https://tobebetterjavaer.com/common-tool/collections.html)[Scanner](https://tobebetterjavaer.com/common-tool/scanner.html) 等,还有一些第三方的工具类,比如说 [Hutool](https://tobebetterjavaer.com/common-tool/hutool.html)[Guava](https://tobebetterjavaer.com/common-tool/guava.html) 等,以及我们今天介绍的 IpUtil、CollectionUtils、StringUtils、MDC、ClassUtils、BeanUtils、ReflectionUtils 等等,在很大程度上能够提高我们的生产效率。
......@@ -42,52 +42,53 @@ public static String getLocalIP() {
本机执行后截图如下:
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/IpUtil-f35dc96f-b8ac-43d3-9393-0ff565e85fb9.jpg)
![](https://cdn.tobebetterjavaer.com/stutymore/utils-20230330093633.png)
阿里云机器执行后截图如下:
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/IpUtil-f50b0de2-cf0d-4e9b-8f10-838ea4b47fd8.jpg)
再问一句,那是否就真的没有问题了呢?在某些情况下,可能返回的是 `127.0.0.1`
在虚拟机中执行时,就可能遇到这个问题,截图如下
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/IpUtil-7c14024b-57d1-4086-9f51-d7bf312b5fbf.jpg)
![](https://cdn.tobebetterjavaer.com/stutymore/utils-20230330095801.png)
#### 2. 进阶版
做一点简单的改动,获取 IpV4 的地址,源码如下
做一点简单的改动,获取 IPV4 的地址,源码如下
```java
/**
* 直接根据第一个网卡地址作为其内网ipv4地址,避免返回 127.0.0.1
*
* @return
*/
public static String getLocalIpByNetcard() {
try {
// 枚举所有的网络接口
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
// 获取当前网络接口
NetworkInterface item = e.nextElement();
// 遍历当前网络接口的所有地址
for (InterfaceAddress address : item.getInterfaceAddresses()) {
// 忽略回环地址和未启用的网络接口
if (item.isLoopback() || !item.isUp()) {
continue;
}
// 如果当前地址是 IPv4 地址,则返回其字符串表示
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
return inet4Address.getHostAddress();
}
}
}
// 如果没有找到任何 IPv4 地址,则返回本地主机地址
return InetAddress.getLocalHost().getHostAddress();
} catch (SocketException | UnknownHostException e) {
// 抛出运行时异常
throw new RuntimeException(e);
}
}
```
需要注意的是,这段代码只返回本机的 IPv4 地址,并且只返回第一个符合条件的地址。如果本机有多个网络接口或者每个接口有多个地址,则可能无法返回预期的地址。此外,如果找不到任何 IPv4 地址,则会返回本地主机地址。
再次测试,输出如下
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/IpUtil-cd2f2acb-a6ea-4675-82a8-95a7e05c8498.jpg)
![](https://cdn.tobebetterjavaer.com/stutymore/utils-20230330100334.png)
#### 3. 完整工具类
......@@ -95,34 +96,45 @@ public static String getLocalIpByNetcard() {
import java.net.*;
import java.util.Enumeration;
public class IpUtil {
public class IPUtil {
public static final String DEFAULT_IP = "127.0.0.1";
/**
* 直接根据第一个网卡地址作为其内网ipv4地址,避免返回 127.0.0.1
*
* @return
* @return 第一个符合条件的内网 IPv4 地址
*/
public static String getLocalIpByNetcard() {
try {
// 枚举所有的网络接口
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
// 获取当前网络接口
NetworkInterface item = e.nextElement();
// 遍历当前网络接口的所有地址
for (InterfaceAddress address : item.getInterfaceAddresses()) {
// 忽略回环地址和未启用的网络接口
if (item.isLoopback() || !item.isUp()) {
continue;
}
// 如果当前地址是 IPv4 地址,则返回其字符串表示
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
return inet4Address.getHostAddress();
}
}
}
// 如果没有找到符合条件的地址,则返回本地主机地址
return InetAddress.getLocalHost().getHostAddress();
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
}
/**
* 获取本地主机地址
*
* @return 本地主机地址
*/
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
......@@ -133,227 +145,13 @@ public class IpUtil {
}
```
### CollectionUtils:Spring 和 Apache 都有提供的集合工具类
对集合操作,除了前面说的`Collections`工具类之后,`CollectionUtils`工具类也非常常用。
目前比较主流的是`spring``org.springframework.util`包下的 CollectionUtils 工具类。
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/CollectionUtils-3433117c-4ab2-4ac4-bf5b-4b729d87fc9a.jpg)
`apache``org.apache.commons.collections`包下的 CollectionUtils 工具类。
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/CollectionUtils-1bc7dfe9-f459-47bb-ae4b-2a25d4be96c1.jpg)
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/CollectionUtils-2b8630a3-141b-4f18-9f54-5a37fc818420.jpg)
> 我个人更推荐使用 apache 的包下的 CollectionUtils 工具类,因为它的工具更多更全面。
举个简单的例子,`spring`的 CollectionUtils 工具类没有判断集合不为空的方法。而`apache`的 CollectionUtils 工具类却有。
下面我们以`apache`的 CollectionUtils 工具类为例,介绍一下常用方法。
#### 集合判空
通过 CollectionUtils 工具类的`isEmpty`方法可以轻松判断集合是否为空,`isNotEmpty`方法判断集合不为空。
```java
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("集合不为空");
}
```
#### 对两个集合进行操作
有时候我们需要对已有的两个集合进行操作,比如取交集或者并集等。
```java
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);
```
执行结果:
```java
[1, 2, 3, 4]
[2]
[1, 3, 4]
[1, 3]
```
说句实话,对两个集合的操作,在实际工作中用得挺多的,特别是很多批量的场景中。以前我们需要写一堆代码,但没想到有现成的轮子。
### StringUtils:专为 Java 字符串而生的工具类
`字符串`(String)在我们的日常工作中,用得非常非常非常多。
在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。
如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。
现在有个好消息是:`org.apache.commons.lang3`包下的`StringUtils`工具类,给我们提供了非常丰富的选择。
#### 字符串判空
其实空字符串,不只是 null 一种,还有""," ","null"等等,多种情况。
StringUtils 给我们提供了多个判空的静态方法,例如:
```java
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));
```
执行结果:
```java
true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true
```
示例中的:`isEmpty``isNotEmpty``isBlank``isNotBlank`,这 4 个判空方法你们可以根据实际情况使用。
> 优先推荐使用`isBlank`和`isNotBlank`方法,因为它会把`" "`也考虑进去。
#### 分隔字符串
分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。
```java
String str1 = null;
System.out.println(StringUtils.split(str1,","));
System.out.println(str1.split(","));
```
执行结果:
```java
null
Exception in thread "main" java.lang.NullPointerException
\tat com.sue.jump.service.test1.UtilTest.main(UtilTest.java:21)
```
使用 StringUtils 的 split 方法会返回 null,而使用 String 的 split 方法会报指针异常。
#### 判断是否纯数字
给定一个字符串,判断它是否为纯数字,可以使用`isNumeric`方法。例如:
```java
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));
```
执行结果:
```java
true
false
false
```
#### 将集合拼接成字符串
有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用`join`方法。例如:
```java
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, " "));
```
执行结果:
```java
a,b,c
1 2 3
```
当然还有很多实用的方法,我在这里就不一一介绍了。
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-68f94af9-d2ea-46c2-81b4-7d7e08891550.jpg)
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-7314260e-4e85-4110-a50d-3bedcbbeb616.jpg)
IPUtil 类中定义了两个方法,分别是 `getLocalIpByNetcard()``getLocalIP()`。前者是获取本机的内网 IPv4 地址,避免了返回 127.0.0.1 的问题。后者是获取本地主机地址,如果本机有多个 IP 地址,则可能返回其中的任意一个。
### MDC:一个线程安全的参数传递工具类
`MDC``org.slf4j`包下的一个类,它的全称是 Mapped Diagnostic Context,我们可以认为它是一个线程安全的存放诊断日志的容器。
`MDC`[`org.slf4j`](https://tobebetterjavaer.com/gongju/slf4j.html) 包下的一个类,它的全称是 Mapped Diagnostic Context,我们可以认为它是一个线程安全的存放诊断日志的容器。
MDC 的底层是用了`ThreadLocal`来保存数据的。
MDC 的底层是用了 [`ThreadLocal`](https://tobebetterjavaer.com/thread/ThreadLocal.html) 来保存数据的。
我们可以用它传递参数。
......@@ -478,13 +276,13 @@ System.out.println(ClassUtils.isInnerClass(User.class));
System.out.println(ClassUtils.isCglibProxy(new User()));
```
ClassUtils 还有很多有用的方法,等待着你去发掘。感兴趣的朋友,可以看看下面内容:
ClassUtils 还有很多有用的方法,等待着你去发掘。感兴趣的小伙伴,可以看看下面的内容:
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-c58920ac-cf04-4d95-ad29-90339a086569.jpg)
### BeanUtils
spring 给我们提供了一个`JavaBean`的工具类,它在`org.springframework.beans`包下面,它的名字叫做:`BeanUtils`
Spring 给我们提供了一个`JavaBean`的工具类,它在`org.springframework.beans`包下面,它的名字叫做:`BeanUtils`
让我们一起看看这个工具可以带给我们哪些惊喜。
......@@ -495,8 +293,8 @@ spring 给我们提供了一个`JavaBean`的工具类,它在`org.springframewo
```java
User user1 = new User();
user1.setId(1L);
user1.setName("苏三说技术");
user1.setAddress("成都");
user1.setName("沉默王二");
user1.setAddress("中国");
User user2 = new User();
BeanUtils.copyProperties(user1, user2);
......@@ -539,7 +337,7 @@ System.out.println(propertyForMethod.getName());
有时候,我们需要在项目中使用`反射`功能,如果使用最原始的方法来开发,代码量会非常多,而且很麻烦,它需要处理一大堆异常以及访问权限等问题。
好消息是 spring 给我们提供了一个`ReflectionUtils`工具,它在`org.springframework.util`包下面。
好消息是 Spring 给我们提供了一个`ReflectionUtils`工具,它在`org.springframework.util`包下面。
#### 获取方法
......@@ -587,6 +385,8 @@ System.out.println(ReflectionUtils.isEqualsMethod(method));
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-0a4ecb9c-b9d2-4090-a7b7-c626a0672b94.jpg)
>参考链接:[https://juejin.cn/post/7102418518599008286](https://juejin.cn/post/7102418518599008286) 作者:苏三,编辑:沉默王二
---
最近整理了一份牛逼的学习资料,包括但不限于 Java 基础部分(JVM、Java 集合框架、多线程),还囊括了 **数据库、计算机网络、算法与数据结构、设计模式、框架类 Spring、Netty、微服务(Dubbo,消息队列) 网关** 等等等等……详情戳:[可以说是 2022 年全网最全的学习和找工作的 PDF 资源了](https://tobebetterjavaer.com/pdf/programmer-111.html)
......
......@@ -208,11 +208,12 @@ head:
- [Java Scanner:扫描控制台输入的工具类](common-tool/scanner.md)
- [Java Arrays:专为数组而生的工具类](common-tool/arrays.md)
- [Apache StringUtils:专为Java字符串而生的工具类](common-tool/StringUtils.md)
- [Objects:专为操作Java对象而生的工具类](common-tool/Objects.md)
- [Java Collections:专为集合而生的工具类](common-tool/collections.md)
- [Hutool:国产良心工具包,让你的Java变得更甜](common-tool/hutool.md)
- [Guava:Google开源的Java工具库,太强大了](common-tool/guava.md)
- [其他常用Java工具类:IpUtil、CollectionUtils、StringUtils、MDC、ClassUtils、BeanUtils、ReflectionUtils](common-tool/utils.md)
- [其他常用Java工具类:IpUtil、MDC、ClassUtils、BeanUtils、ReflectionUtils](common-tool/utils.md)
### Java新特性
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册